This is an automated email from the git hooks/post-receive script.

git pushed a commit to reference refs/pull/197/head
in repository enlightenment.

View the commit online.

commit 2179eb4d0616d6a355fc7d6d7b89263365acf993
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 21 11:47:54 2026 -0600

    e_comp_wl - survive a client shrinking the file behind its shm pool
    
    Any Wayland client can kill the compositor: make a wl_shm buffer,
    truncate the file behind the pool, and commit the buffer. Evas reads
    the mapping past the end of the file in a render thread, SIGBUS
    follows, and eina_mmap's handler, which only knows EFL's own mapped
    files, aborts.
    
    wl_shm_buffer_begin_access() is libwayland's answer, as weston uses
    it, but it only covers the thread that calls it, and the reads happen
    later in evas's render threads. So this does what wlroots does: E
    records every shm buffer it holds for rendering, for as long as it
    holds the pool, and its SIGBUS handler maps a page of zeroes over a
    faulting page inside one of them. The render reads garbage instead of
    dying, and once it is over the client gets wl_shm.error.invalid_fd on
    the wl_buffer, which is what wlcs expects.
    
    The records follow the pool references e_pixmap already takes to keep
    a buffer alive across a render, and the handler runs on whichever
    thread faulted, so they are only touched under a lock that is never
    held while pool memory is read. A fault anywhere else goes to the
    handler installed before, which is made to be eina_mmap's so that
    EFL's own files keep their protection.
    
    e_pixmap.c is shared, but only its Wayland buffer paths change. The
    default suite and the X11 suite pass. The new wl-shm-truncate test
    fails without this, with the compositor gone, and passes 40 runs out
    of 40 with it. wlcs BadBufferTest.test_truncated_shm_file now passes.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bin/e_comp_wl.h                   |   3 +
 src/bin/e_comp_wl_shm.c               | 246 ++++++++++++++++++++++++++++++++++
 src/bin/e_pixmap.c                    |  13 +-
 src/bin/meson.build                   |   1 +
 src/tests/wayland/e_wl_testkit.c      |   6 +
 src/tests/wayland/e_wl_testkit.h      |   4 +
 src/tests/wayland/meson.build         |   1 +
 src/tests/wayland/test_shm_truncate.c | 112 ++++++++++++++++
 8 files changed, 384 insertions(+), 2 deletions(-)

diff --git a/src/bin/e_comp_wl.h b/src/bin/e_comp_wl.h
index e4f98e954..8a5b515d5 100644
--- a/src/bin/e_comp_wl.h
+++ b/src/bin/e_comp_wl.h
@@ -419,6 +419,9 @@ EINTERN void e_comp_wl_surface_destroy(struct wl_resource *resource);
 EINTERN Eina_Bool e_comp_wl_surface_commit(E_Client *ec);
 EINTERN Eina_Bool e_comp_wl_subsurface_commit(E_Client *ec);
 E_API E_Comp_Wl_Buffer *e_comp_wl_buffer_get(struct wl_resource *resource);
+EINTERN void e_comp_wl_shm_guard_add(struct wl_shm_pool *pool, E_Comp_Wl_Buffer *buffer);
+EINTERN void e_comp_wl_shm_guard_del(struct wl_shm_pool *pool);
+EINTERN void e_comp_wl_shm_faults_post(void);
 
 E_API struct wl_signal e_comp_wl_surface_create_signal_get(void);
 E_API double e_comp_wl_idle_time_get(void);
diff --git a/src/bin/e_comp_wl_shm.c b/src/bin/e_comp_wl_shm.c
new file mode 100644
index 000000000..85374de2b
--- /dev/null
+++ b/src/bin/e_comp_wl_shm.c
@@ -0,0 +1,246 @@
+#define E_COMP_WL
+#include "e.h"
+
+#include <signal.h>
+#include <sys/mman.h>
+
+/* A client can shrink the file behind its wl_shm pool at any time, and
+ * reading a page of the mapping past the new end of the file raises
+ * SIGBUS. Evas reads shm buffers from its render threads, after the
+ * commit that attached them, so the reads cannot be bracketed with
+ * wl_shm_buffer_begin_access(), which only covers the calling thread.
+ *
+ * Instead, as wlroots does, every buffer e_pixmap holds for rendering is
+ * recorded here for as long as it holds the buffer's pool. A fault inside
+ * one gets a page of zeroes mapped over the faulting page, so the render
+ * reads garbage instead of the compositor dying, and the client gets
+ * wl_shm.error.invalid_fd on that wl_buffer once the render is over, as
+ * wlcs's BadBufferTest.test_truncated_shm_file expects.
+ *
+ * The handler runs on whichever thread faulted, so the records are only
+ * touched under _shm_lock, and nothing reads pool memory while holding it.
+ * Any other fault is passed to the handler installed before this one. */
+
+typedef struct
+{
+   uintptr_t start, end;
+   struct wl_resource *buffer;
+   struct wl_listener buffer_destroy;
+   Eina_Bool faulted;
+} E_Comp_Wl_Shm_Range;
+
+typedef struct
+{
+   struct wl_shm_pool *pool;
+   unsigned int refs;
+   Eina_List *ranges;
+} E_Comp_Wl_Shm_Pool_Guard;
+
+static Eina_Spinlock _shm_lock;
+static Eina_List *_shm_pool_guards = NULL;
+static struct sigaction _shm_prev_sigbus;
+static uintptr_t _shm_page_size = 0;
+static Eina_Bool _shm_handler_installed = EINA_FALSE;
+
+static Eina_Bool
+_e_comp_wl_shm_fault_absorb(uintptr_t addr)
+{
+   E_Comp_Wl_Shm_Pool_Guard *guard;
+   E_Comp_Wl_Shm_Range *range;
+   Eina_List *l, *ll;
+   Eina_Bool absorbed = EINA_FALSE;
+   void *page;
+
+   eina_spinlock_take(&_shm_lock);
+   EINA_LIST_FOREACH(_shm_pool_guards, l, guard)
+     EINA_LIST_FOREACH(guard->ranges, ll, range)
+       {
+          if ((addr < range->start) || (addr >= range->end)) continue;
+          page = (void *)(addr & ~(_shm_page_size - 1));
+          if (mmap(page, _shm_page_size, PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) != MAP_FAILED)
+            {
+               range->faulted = EINA_TRUE;
+               absorbed = EINA_TRUE;
+            }
+          goto done;
+       }
+done:
+   eina_spinlock_release(&_shm_lock);
+   return absorbed;
+}
+
+static void
+_e_comp_wl_shm_sigbus(int sig, siginfo_t *info, void *context)
+{
+   struct sigaction dfl;
+   int saved_errno = errno;
+
+   if ((info->si_code == BUS_ADRERR) &&
+       _e_comp_wl_shm_fault_absorb((uintptr_t)info->si_addr))
+     {
+        errno = saved_errno;
+        return;
+     }
+   errno = saved_errno;
+
+   if (_shm_prev_sigbus.sa_flags & SA_SIGINFO)
+     {
+        _shm_prev_sigbus.sa_sigaction(sig, info, context);
+        return;
+     }
+   if (_shm_prev_sigbus.sa_handler == SIG_IGN) return;
+   if (_shm_prev_sigbus.sa_handler != SIG_DFL)
+     {
+        _shm_prev_sigbus.sa_handler(sig);
+        return;
+     }
+   /* Returning re-runs the faulting access, which now gets the default. */
+   memset(&dfl, 0, sizeof(dfl));
+   dfl.sa_handler = SIG_DFL;
+   sigaction(SIGBUS, &dfl, NULL);
+}
+
+static Eina_Bool
+_e_comp_wl_shm_handler_install(void)
+{
+   struct sigaction sa;
+
+   if (_shm_handler_installed) return EINA_TRUE;
+
+   /* eina_mmap's handler aborts on a fault outside its own files without
+    * passing it on, so it has to be the one this handler passes faults to,
+    * never the one installed over it. It is installed the first time EFL
+    * maps a file; make sure that has happened. */
+   eina_mmap_safety_enabled_set(EINA_TRUE);
+
+   if (!eina_spinlock_new(&_shm_lock)) return EINA_FALSE;
+   _shm_page_size = (uintptr_t)sysconf(_SC_PAGESIZE);
+
+   memset(&sa, 0, sizeof(sa));
+   sa.sa_sigaction = _e_comp_wl_shm_sigbus;
+   sa.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESTART;
+   sigemptyset(&sa.sa_mask);
+   if (sigaction(SIGBUS, &sa, &_shm_prev_sigbus))
+     {
+        ERR("Cannot install a SIGBUS handler for shm buffers: %m");
+        eina_spinlock_free(&_shm_lock);
+        return EINA_FALSE;
+     }
+   _shm_handler_installed = EINA_TRUE;
+   return EINA_TRUE;
+}
+
+static void
+_e_comp_wl_shm_cb_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
+{
+   E_Comp_Wl_Shm_Range *range;
+
+   range = container_of(listener, E_Comp_Wl_Shm_Range, buffer_destroy);
+   range->buffer = NULL;
+   wl_list_remove(&listener->link);
+   wl_list_init(&listener->link);
+}
+
+static void
+_e_comp_wl_shm_fault_post(E_Comp_Wl_Shm_Range *range)
+{
+   if (!range->faulted) return;
+   range->faulted = EINA_FALSE;
+   if (!range->buffer)
+     {
+        ERR("A client shrank the file behind a shm buffer it has destroyed");
+        return;
+     }
+   wl_resource_post_error(range->buffer, WL_SHM_ERROR_INVALID_FD,
+                          "the file behind this buffer's pool was shrunk");
+}
+
+static E_Comp_Wl_Shm_Pool_Guard *
+_e_comp_wl_shm_pool_guard_find(struct wl_shm_pool *pool)
+{
+   E_Comp_Wl_Shm_Pool_Guard *guard;
+   Eina_List *l;
+
+   EINA_LIST_FOREACH(_shm_pool_guards, l, guard)
+     if (guard->pool == pool) return guard;
+   return NULL;
+}
+
+/* Called with each reference e_pixmap takes on a buffer's pool to render
+ * it. The pool keeps the whole mapping alive, so every buffer recorded for
+ * it stays covered until the last of those references is dropped. */
+EINTERN void
+e_comp_wl_shm_guard_add(struct wl_shm_pool *pool, E_Comp_Wl_Buffer *buffer)
+{
+   E_Comp_Wl_Shm_Pool_Guard *guard;
+   E_Comp_Wl_Shm_Range *range;
+   struct wl_shm_buffer *shm = buffer->shm_buffer;
+
+   if (!_e_comp_wl_shm_handler_install()) return;
+
+   range = E_NEW(E_Comp_Wl_Shm_Range, 1);
+   range->start = (uintptr_t)wl_shm_buffer_get_data(shm);
+   range->end = range->start +
+     ((size_t)wl_shm_buffer_get_stride(shm) * (size_t)wl_shm_buffer_get_height(shm));
+   range->buffer = buffer->resource;
+   range->buffer_destroy.notify = _e_comp_wl_shm_cb_buffer_destroy;
+   wl_resource_add_destroy_listener(buffer->resource, &range->buffer_destroy);
+
+   eina_spinlock_take(&_shm_lock);
+   guard = _e_comp_wl_shm_pool_guard_find(pool);
+   if (!guard)
+     {
+        guard = E_NEW(E_Comp_Wl_Shm_Pool_Guard, 1);
+        guard->pool = pool;
+        _shm_pool_guards = eina_list_append(_shm_pool_guards, guard);
+     }
+   guard->refs++;
+   guard->ranges = eina_list_append(guard->ranges, range);
+   eina_spinlock_release(&_shm_lock);
+}
+
+/* Called before each of those references is dropped. */
+EINTERN void
+e_comp_wl_shm_guard_del(struct wl_shm_pool *pool)
+{
+   E_Comp_Wl_Shm_Pool_Guard *guard;
+   E_Comp_Wl_Shm_Range *range;
+
+   if (!_shm_handler_installed) return;
+
+   eina_spinlock_take(&_shm_lock);
+   guard = _e_comp_wl_shm_pool_guard_find(pool);
+   if ((!guard) || (--guard->refs))
+     {
+        eina_spinlock_release(&_shm_lock);
+        return;
+     }
+   _shm_pool_guards = eina_list_remove(_shm_pool_guards, guard);
+   eina_spinlock_release(&_shm_lock);
+
+   EINA_LIST_FREE(guard->ranges, range)
+     {
+        _e_comp_wl_shm_fault_post(range);
+        wl_list_remove(&range->buffer_destroy.link);
+        free(range);
+     }
+   free(guard);
+}
+
+/* Tells the clients whose buffers faulted during the render just over. */
+EINTERN void
+e_comp_wl_shm_faults_post(void)
+{
+   E_Comp_Wl_Shm_Pool_Guard *guard;
+   E_Comp_Wl_Shm_Range *range;
+   Eina_List *l, *ll;
+
+   if (!_shm_handler_installed) return;
+
+   eina_spinlock_take(&_shm_lock);
+   EINA_LIST_FOREACH(_shm_pool_guards, l, guard)
+     EINA_LIST_FOREACH(guard->ranges, ll, range)
+       _e_comp_wl_shm_fault_post(range);
+   eina_spinlock_release(&_shm_lock);
+}
diff --git a/src/bin/e_pixmap.c b/src/bin/e_pixmap.c
index 52ece88e7..0967d4a74 100644
--- a/src/bin/e_pixmap.c
+++ b/src/bin/e_pixmap.c
@@ -139,6 +139,13 @@ _e_pixmap_image_clear_x(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_U
 #endif
 
 #ifdef HAVE_WAYLAND
+static void
+_e_pixmap_shm_pool_unref(struct wl_shm_pool *pool)
+{
+   e_comp_wl_shm_guard_del(pool);
+   wl_shm_pool_unref(pool);
+}
+
 static void
 _e_pixmap_wl_resource_release(E_Comp_Wl_Buffer *buffer)
 {
@@ -158,7 +165,7 @@ _e_pixmap_wl_resource_release(E_Comp_Wl_Buffer *buffer)
           _render_held_pools = eina_list_append(_render_held_pools,
                                                 buffer->pool);
         else
-          wl_shm_pool_unref(buffer->pool);
+          _e_pixmap_shm_pool_unref(buffer->pool);
         buffer->pool = NULL;
      }
 
@@ -216,8 +223,9 @@ e_pixmap_render_done(void)
 #ifdef HAVE_WAYLAND
    struct wl_shm_pool *pool;
 
+   e_comp_wl_shm_faults_post();
    EINA_LIST_FREE(_render_held_pools, pool)
-     wl_shm_pool_unref(pool);
+     _e_pixmap_shm_pool_unref(pool);
 #endif
 }
 
@@ -1023,6 +1031,7 @@ e_pixmap_image_refresh(E_Pixmap *cp)
 
            cp->held_buffer->pool =
              wl_shm_buffer_ref_pool(cp->held_buffer->shm_buffer);
+           e_comp_wl_shm_guard_add(cp->held_buffer->pool, cp->held_buffer);
            cp->held_buffer->busy++;
            cp->data = ""
            cp->held_buffer_destroy_listener.notify =
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 41d174b3d..52a21b9d1 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -423,6 +423,7 @@ if config_h.has('HAVE_WAYLAND') == true
     'e_comp_wl.c',
     'e_comp_wl_extensions.c',
     'e_comp_wl_extensions_tizen.c',
+    'e_comp_wl_shm.c',
     wayland_proto_c,
     wayland_proto_h
   ]
diff --git a/src/tests/wayland/e_wl_testkit.c b/src/tests/wayland/e_wl_testkit.c
index 5b5fcff7c..aaf6d8654 100644
--- a/src/tests/wayland/e_wl_testkit.c
+++ b/src/tests/wayland/e_wl_testkit.c
@@ -560,6 +560,12 @@ tk_sync(Tk *tk)
      }
 }
 
+struct wl_display *
+tk_display(Tk *tk)
+{
+   return tk->disp;
+}
+
 void
 tk_toplevel_configured(Tk_Toplevel *top, int *w, int *h, int *count)
 {
diff --git a/src/tests/wayland/e_wl_testkit.h b/src/tests/wayland/e_wl_testkit.h
index 4514ff775..538526d00 100644
--- a/src/tests/wayland/e_wl_testkit.h
+++ b/src/tests/wayland/e_wl_testkit.h
@@ -117,6 +117,10 @@ void *tk_bind(Tk *tk, const struct wl_interface *iface, uint32_t version);
  * a job or an idler have landed. */
 void tk_sync(Tk *tk);
 
+/* For a test that has to dispatch on its own, waiting for an event the kit
+ * does not track. */
+struct wl_display *tk_display(Tk *tk);
+
 /* Sync until a change that needs the client's answer has landed: one round
  * trip carries the configure out and the commit back, one more lets the
  * compositor apply it. */
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index 093382b33..31f493a96 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -68,6 +68,7 @@ wl_protocol_tests = [
   ['xdg-toplevel-v6', 'test_xdg_toplevel_v6.c'],
   ['maximize-restore', 'test_maximize_restore.c'],
   ['xdg-decoration', 'test_xdg_decoration.c'],
+  ['shm-truncate', 'test_shm_truncate.c'],
 ]
 
 tk_src = files('e_wl_testkit.c')
diff --git a/src/tests/wayland/test_shm_truncate.c b/src/tests/wayland/test_shm_truncate.c
new file mode 100644
index 000000000..5da7f35ca
--- /dev/null
+++ b/src/tests/wayland/test_shm_truncate.c
@@ -0,0 +1,112 @@
+/* A client that shrinks the file behind its wl_shm pool, after creating a
+ * buffer in it, must not take the compositor down when the buffer is
+ * drawn. It gets wl_shm.error.invalid_fd on that wl_buffer, and other
+ * clients carry on. */
+#define _GNU_SOURCE
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "e_wl_testkit.h"
+
+#define PROG "test-shm-truncate"
+#define APP_ID "e.test.shm_truncate"
+#define W 200
+#define H 150
+
+static void
+_frame_done(void *data, struct wl_callback *cb, uint32_t t)
+{
+   int *done = data;
+
+   (void)t;
+   wl_callback_destroy(cb);
+   *done = 1;
+}
+
+static const struct wl_callback_listener _frame_listener = { _frame_done };
+
+/* A buffer whose pool's file is emptied right after the buffer is made. */
+static struct wl_buffer *
+_truncated_buffer_make(Tk *tk)
+{
+   struct wl_shm *shm = tk_bind(tk, &wl_shm_interface, 1);
+   struct wl_shm_pool *pool;
+   struct wl_buffer *buffer;
+   size_t size = W * H * 4;
+   int fd;
+
+   fd = memfd_create(PROG, MFD_CLOEXEC);
+   if (fd < 0) tk_fail(tk, "memfd_create: %m");
+   if (ftruncate(fd, size) < 0) tk_fail(tk, "ftruncate: %m");
+   pool = wl_shm_create_pool(shm, fd, size);
+   buffer = wl_shm_pool_create_buffer(pool, 0, W, H, W * 4,
+                                      WL_SHM_FORMAT_ARGB8888);
+   wl_shm_pool_destroy(pool);
+   wl_display_roundtrip(tk_display(tk));
+   if (ftruncate(fd, 0) < 0) tk_fail(tk, "ftruncate: %m");
+   close(fd);
+   return buffer;
+}
+
+int
+main(void)
+{
+   Tk *tk, *other;
+   Tk_Toplevel *top;
+   struct wl_surface *surface;
+   struct wl_buffer *buffer;
+   const struct wl_interface *iface = NULL;
+   int frames, done, code;
+
+   tk = tk_connect(PROG);
+   top = tk_toplevel_new(tk, APP_ID, "shm truncate", W, H);
+   tk_expect(tk, APP_ID);
+   surface = tk_toplevel_surface(top);
+
+   buffer = _truncated_buffer_make(tk);
+   wl_surface_attach(surface, buffer, 0, 0);
+   /* A frame callback can be answered by a render that was already under
+    * way when the commit arrived, so a frame done does not prove the
+    * buffer was read: draw it again until the error or the frames run
+    * out. */
+   for (frames = 0; frames < 5; frames++)
+     {
+        done = 0;
+        wl_surface_damage(surface, 0, 0, W, H);
+        wl_callback_add_listener(wl_surface_frame(surface), &_frame_listener,
+                                 &done);
+        wl_surface_commit(surface);
+        while (!done)
+          if (wl_display_dispatch(tk_display(tk)) < 0) break;
+        if (!done) break;
+     }
+
+   if (done)
+     tk_fail(tk, "a buffer whose file was truncated was drawn %d times with "
+             "no error", frames);
+   code = tk_protocol_error(tk, &iface);
+   if (code < 0)
+     {
+        fprintf(stderr, PROG ": the connection ended without a protocol "
+                "error: %s\n", strerror(wl_display_get_error(tk_display(tk))));
+        return 1;
+     }
+   if ((iface != &wl_buffer_interface) || (code != WL_SHM_ERROR_INVALID_FD))
+     {
+        fprintf(stderr, PROG ": expected wl_shm.error.invalid_fd on the "
+                "wl_buffer, got error %d on %s\n", code,
+                iface ? iface->name : "nothing");
+        return 1;
+     }
+
+   other = tk_connect(PROG);
+   tk_toplevel_new(other, APP_ID ".other", "still here", W, H);
+   tk_expect(other, APP_ID ".other");
+
+   printf(PROG ": ok\n");
+   tk_disconnect(other);
+   return 0;
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to