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

git pushed a commit to branch v-1.29
in repository efl.

View the commit online.

commit c276b8637ff2a50811608094948515445e4e656b
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 09:21:52 2026 -0600

    ecore: keep freed fd handlers off the prepare list
    
    ecore_main_fd_handler_prepare_callback_set() put a handler on the
    prepare list even when it was given a NULL callback, and delete and
    cleanup took a handler off that list only when it had a prepare
    callback. Efl_Loop_Handler sets a NULL callback on every fd handler it
    makes, so they all landed there. The prepare pass dropped such entries
    before the next select, which hid the problem, but a handler deleted
    before that pass was freed by cleanup while still on the list, and the
    pass then read freed memory. A program that closes fds before deleting
    their handlers gets two loop rebuilds (epoll EBADF) in one iteration:
    the clock change watcher is made and deleted again, and the pass calls
    through a garbage pointer once the memory is reused.
    
    Each handler now records whether it is on the list, and that flag is
    cleared wherever the list is freed. Delete and cleanup take it off
    whenever it is, a NULL callback is not added, and setting NULL takes
    the handler off.
    
    The prepare pass walked the list with EINA_LIST_FOREACH_SAFE, but a
    prepare callback that deletes the next handler in the walk frees the
    node the walk holds. As ecore does for its other lists, nothing leaves
    the list while it is walked: removals are deferred and the outermost
    walk sweeps the list once done. Cleanup does not free a listed handler
    during a walk either, for callbacks that iterate the loop.
    
    The first test checks that a deleted handler is off the list. The
    other two, a callback deleting the next handler and a callback that
    changes the list and iterates the loop, hang without the fix.
---
 src/lib/ecore/ecore_main.c         |  93 +++++++++++------
 src/lib/ecore/ecore_private.h      |   2 +
 src/tests/ecore/ecore_test_ecore.c | 208 +++++++++++++++++++++++++++++++++++++
 3 files changed, 270 insertions(+), 33 deletions(-)

diff --git a/src/lib/ecore/ecore_main.c b/src/lib/ecore/ecore_main.c
index 63a3c068ad..23b1ad1f06 100644
--- a/src/lib/ecore/ecore_main.c
+++ b/src/lib/ecore/ecore_main.c
@@ -145,6 +145,7 @@ struct _Ecore_Fd_Handler
    Eina_Bool               delete_me : 1;
    Eina_Bool               file : 1;
    Eina_Bool               legacy : 1;
+   Eina_Bool               with_prep : 1;
 };
 GENERIC_ALLOC_SIZE_DECLARE(Ecore_Fd_Handler);
 
@@ -170,6 +171,7 @@ static int  _ecore_main_select(Eo *obj, Efl_Loop_Data *pd, double timeout);
 #endif
 static void _ecore_main_prepare_handlers(Eo *obj, Efl_Loop_Data *pd);
 static void _ecore_main_fd_handlers_cleanup(Eo *obj, Efl_Loop_Data *pd);
+static void _ecore_main_fd_handler_prep_unlist(Efl_Loop_Data *pd, Ecore_Fd_Handler *fdh);
 #ifndef _WIN32
 # ifndef USE_G_MAIN_LOOP
 static void _ecore_main_fd_handlers_bads_rem(Eo *obj, Efl_Loop_Data *pd);
@@ -1429,9 +1431,7 @@ _ecore_main_fd_handler_del(Eo *obj EINA_UNUSED,
         _ecore_main_fdh_poll_del(pd, fd_handler);
         pd->fd_handlers_to_delete = eina_list_append
           (pd->fd_handlers_to_delete, fd_handler);
-        if (fd_handler->prep_func && pd->fd_handlers_with_prep)
-          pd->fd_handlers_with_prep = eina_list_remove
-            (pd->fd_handlers_with_prep, fd_handler);
+        _ecore_main_fd_handler_prep_unlist(pd, fd_handler);
         if (fd_handler->buf_func && pd->fd_handlers_with_buffer)
           pd->fd_handlers_with_buffer = eina_list_remove
             (pd->fd_handlers_with_buffer, fd_handler);
@@ -1615,12 +1615,15 @@ ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler,
      }
    fd_handler->prep_func = func;
    fd_handler->prep_data = (void *)data;
-   if ((!pd->fd_handlers_with_prep) ||
-       (pd->fd_handlers_with_prep &&
-        (!eina_list_data_find(pd->fd_handlers_with_prep, fd_handler))))
-     // FIXME: THIS WILL NOT SCALE WITH LOTS OF PREP FUNCTIONS!!!
-     pd->fd_handlers_with_prep = eina_list_append
-       (pd->fd_handlers_with_prep, fd_handler);
+   if (!func)
+     _ecore_main_fd_handler_prep_unlist(pd, fd_handler);
+   else if (!fd_handler->with_prep)
+     {
+        // FIXME: THIS WILL NOT SCALE WITH LOTS OF PREP FUNCTIONS!!!
+        pd->fd_handlers_with_prep = eina_list_append
+          (pd->fd_handlers_with_prep, fd_handler);
+        fd_handler->with_prep = EINA_TRUE;
+     }
 }
 
 EAPI int
@@ -1679,6 +1682,15 @@ ecore_main_fd_handler_active_set(Ecore_Fd_Handler      *fd_handler,
          fd_handler->fd, fd_handler->loop_data, strerror(errno));
 }
 
+static void
+_ecore_main_fd_handlers_with_prep_free(Efl_Loop_Data *pd)
+{
+   Ecore_Fd_Handler *fdh;
+
+   EINA_LIST_FREE(pd->fd_handlers_with_prep, fdh)
+     fdh->with_prep = EINA_FALSE;
+}
+
 void
 _ecore_main_content_clear(Eo *obj, Efl_Loop_Data *pd)
 {
@@ -1703,9 +1715,7 @@ _ecore_main_content_clear(Eo *obj, Efl_Loop_Data *pd)
    if (pd->fd_handlers_with_buffer)
      pd->fd_handlers_with_buffer =
        eina_list_free(pd->fd_handlers_with_buffer);
-   if (pd->fd_handlers_with_prep)
-     pd->fd_handlers_with_prep =
-       eina_list_free(pd->fd_handlers_with_prep);
+   _ecore_main_fd_handlers_with_prep_free(pd);
    if (pd->file_fd_handlers)
      pd->file_fd_handlers =
        eina_list_free(pd->file_fd_handlers);
@@ -1754,30 +1764,48 @@ _ecore_main_shutdown(void)
      }
 }
 
+// A prepare callback may delete any fd handler or clear its prepare
+// callback, the next one in the walk included. So nothing leaves the list
+// while it is walked: the outermost walk sweeps the list once it is done.
+static void
+_ecore_main_fd_handler_prep_unlist(Efl_Loop_Data *pd, Ecore_Fd_Handler *fdh)
+{
+   if (!fdh->with_prep) return;
+   if (pd->fd_handlers_with_prep_walking)
+     {
+        pd->fd_handlers_with_prep_dirty = EINA_TRUE;
+        return;
+     }
+   pd->fd_handlers_with_prep = eina_list_remove
+     (pd->fd_handlers_with_prep, fdh);
+   fdh->with_prep = EINA_FALSE;
+}
+
 static void
 _ecore_main_prepare_handlers(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
 {
    Ecore_Fd_Handler *fdh;
    Eina_List *l, *l2;
 
-   // call the prepare callback for all handlers with prep functions
+   pd->fd_handlers_with_prep_walking++;
+   EINA_LIST_FOREACH(pd->fd_handlers_with_prep, l, fdh)
+     {
+        if (fdh->delete_me || !fdh->prep_func) continue;
+        fdh->references++;
+        _ecore_call_prep_cb(fdh->prep_func, fdh->prep_data, fdh);
+        fdh->references--;
+     }
+   pd->fd_handlers_with_prep_walking--;
+
+   if (pd->fd_handlers_with_prep_walking) return;
+   if (!pd->fd_handlers_with_prep_dirty) return;
+   pd->fd_handlers_with_prep_dirty = EINA_FALSE;
    EINA_LIST_FOREACH_SAFE(pd->fd_handlers_with_prep, l, l2, fdh)
      {
-        if (!fdh)
-          {
-             pd->fd_handlers_with_prep = eina_list_remove_list
-               (l, pd->fd_handlers_with_prep);
-             continue;
-          }
-        if (!fdh->delete_me && fdh->prep_func)
-          {
-             fdh->references++;
-             _ecore_call_prep_cb(fdh->prep_func, fdh->prep_data, fdh);
-             fdh->references--;
-          }
-        else
-          pd->fd_handlers_with_prep = eina_list_remove_list
-            (pd->fd_handlers_with_prep, l);
+        if (!fdh->delete_me && fdh->prep_func) continue;
+        pd->fd_handlers_with_prep = eina_list_remove_list
+          (pd->fd_handlers_with_prep, l);
+        fdh->with_prep = EINA_FALSE;
      }
 }
 
@@ -2087,14 +2115,14 @@ _ecore_main_fd_handlers_cleanup(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
              continue;
           }
         if (fdh->references) continue;
+        // iterating from a prepare callback: the walk still holds its node
+        if (fdh->with_prep && pd->fd_handlers_with_prep_walking) continue;
         if (pd->fd_handlers_to_call_current == fdh)
           pd->fd_handlers_to_call_current = NULL;
         if (fdh->buf_func && pd->fd_handlers_with_buffer)
           pd->fd_handlers_with_buffer = eina_list_remove
             (pd->fd_handlers_with_buffer, fdh);
-        if (fdh->prep_func && pd->fd_handlers_with_prep)
-          pd->fd_handlers_with_prep = eina_list_remove
-           (pd->fd_handlers_with_prep, fdh);
+        _ecore_main_fd_handler_prep_unlist(pd, fdh);
         pd->fd_handlers = (Ecore_Fd_Handler *)
           eina_inlist_remove(EINA_INLIST_GET(pd->fd_handlers),
                              EINA_INLIST_GET(fdh));
@@ -2246,6 +2274,7 @@ _ecore_main_loop_uv_prepare(uv_prepare_t *handle EINA_UNUSED)
    if ((pd->in_loop == 0) && (pd->do_quit))
      {
         _ecore_main_fd_handlers_cleanup(obj, pd);
+        _ecore_main_fd_handlers_with_prep_free(pd);
 
         while (pd->fd_handlers)
           {
@@ -2260,8 +2289,6 @@ _ecore_main_loop_uv_prepare(uv_prepare_t *handle EINA_UNUSED)
           }
        if (pd->fd_handlers_with_buffer)
          pd->fd_handlers_with_buffer = eina_list_free(pd->fd_handlers_with_buffer);
-       if (pd->fd_handlers_with_prep)
-         pd->fd_handlers_with_prep = eina_list_free(pd->fd_handlers_with_prep);
        if (pd->fd_handlers_to_delete)
          pd->fd_handlers_to_delete = eina_list_free(pd->fd_handlers_to_delete);
        if (pd->file_fd_handlers)
diff --git a/src/lib/ecore/ecore_private.h b/src/lib/ecore/ecore_private.h
index 16f4c33f93..7babc8eef2 100644
--- a/src/lib/ecore/ecore_private.h
+++ b/src/lib/ecore/ecore_private.h
@@ -125,6 +125,7 @@ struct _Efl_Loop_Data
    Eina_List           *fd_handlers_to_delete;
    Ecore_Fd_Handler    *fd_handlers_to_call;
    Ecore_Fd_Handler    *fd_handlers_to_call_current;
+   unsigned int         fd_handlers_with_prep_walking;
 
 # ifdef _WIN32
    Ecore_Win32_Handler *win32_handlers;
@@ -169,6 +170,7 @@ struct _Efl_Loop_Data
 
    Eina_Bool            do_quit : 1;
    Eina_Bool            quit_on_last_thread_child_del : 1;
+   Eina_Bool            fd_handlers_with_prep_dirty : 1;
 };
 
 struct _Efl_Task_Data
diff --git a/src/tests/ecore/ecore_test_ecore.c b/src/tests/ecore/ecore_test_ecore.c
index 66544d0f98..c276caada9 100644
--- a/src/tests/ecore/ecore_test_ecore.c
+++ b/src/tests/ecore/ecore_test_ecore.c
@@ -13,6 +13,7 @@
 #include <Ecore.h>
 
 #include "ecore_suite.h"
+#include "ecore_private.h"
 
 #define ECORE_EVENT_CUSTOM_1 1
 #define ECORE_EVENT_CUSTOM_2 2
@@ -183,6 +184,210 @@ EFL_START_TEST(ecore_test_ecore_main_loop_fd_handler_activate_modify)
 }
 EFL_END_TEST
 
+static void
+_prepare_null_quit_job_cb(void *data EINA_UNUSED)
+{
+   ecore_main_loop_quit();
+}
+
+// Efl_Loop_Handler sets a NULL prepare callback on every fd handler it
+// creates, and deletes the handler without ever setting a real one.
+static void
+_prepare_null_del_job_cb(void *data)
+{
+   Efl_Loop_Data *pd = efl_data_scope_get(efl_main_loop_get(),
+                                          EFL_LOOP_CLASS);
+   Ecore_Fd_Handler *fdh;
+   int *fd = data;
+
+   fdh = ecore_main_fd_handler_add(*fd, ECORE_FD_READ, _fd_handler_cb,
+                                   NULL, NULL, NULL);
+   ck_assert_ptr_nonnull(fdh);
+   ecore_main_fd_handler_prepare_callback_set(fdh, NULL, NULL);
+   ecore_main_fd_handler_del(fdh);
+   ck_assert(!eina_list_data_find(pd->fd_handlers_with_prep, fdh));
+   ecore_job_add(_prepare_null_quit_job_cb, NULL);
+}
+
+// The handler is freed at the end of the iteration that runs the first job,
+// so it must be off the prepare list once deleted. The next iteration
+// prepares its fd handlers before it runs the second job: a memory checker
+// sees the read of the freed handler if it is still there.
+EFL_START_TEST(ecore_test_ecore_main_loop_fd_handler_prepare_null_del)
+{
+   int comm[2];
+
+   fail_if(pipe(comm) != 0);
+
+   ecore_job_add(_prepare_null_del_job_cb, &comm[0]);
+   ecore_main_loop_begin();
+
+   close(comm[0]);
+   close(comm[1]);
+}
+EFL_END_TEST
+
+typedef struct
+{
+   Ecore_Fd_Handler *fdh[3];
+   int               prepared[3];
+} Prepare_Del_Data;
+
+static void
+_prepare_del_next_cb(void *data, Ecore_Fd_Handler *fdh)
+{
+   Prepare_Del_Data *pdd = data;
+   int i;
+
+   for (i = 0; i < 3; i++)
+     if (pdd->fdh[i] == fdh) pdd->prepared[i]++;
+   if ((fdh == pdd->fdh[0]) && (pdd->fdh[1]))
+     {
+        ecore_main_fd_handler_del(pdd->fdh[1]);
+        pdd->fdh[1] = NULL;
+     }
+}
+
+static Eina_Bool
+_prepare_del_next_read_cb(void *data, Ecore_Fd_Handler *fdh EINA_UNUSED)
+{
+   Eina_Bool *did = data;
+
+   *did = EINA_TRUE;
+   ecore_main_loop_quit();
+   return ECORE_CALLBACK_RENEW;
+}
+
+// The first prepare callback deletes the handler that comes next in the
+// prepare walk. The walk has to skip it and still reach the third one.
+// A walk that loses its next node can spin forever instead of failing an
+// assertion: the suite timeout catches that.
+EFL_START_TEST(ecore_test_ecore_main_loop_fd_handler_prepare_del_next)
+{
+   Prepare_Del_Data pdd = { { NULL }, { 0 } };
+   Eina_Bool did = EINA_FALSE;
+   int comm[3][2];
+   int i;
+
+   for (i = 0; i < 3; i++)
+     {
+        fail_if(pipe(comm[i]) != 0);
+        pdd.fdh[i] = ecore_main_fd_handler_add
+          (comm[i][0], ECORE_FD_READ, _prepare_del_next_read_cb, &did,
+           NULL, NULL);
+        ck_assert_ptr_nonnull(pdd.fdh[i]);
+        ecore_main_fd_handler_prepare_callback_set
+          (pdd.fdh[i], _prepare_del_next_cb, &pdd);
+     }
+   fail_if(write(comm[2][1], "x", 1) != 1);
+
+   ecore_main_loop_begin();
+
+   ck_assert(did);
+   ck_assert_int_ge(pdd.prepared[0], 1);
+   ck_assert_int_eq(pdd.prepared[1], 0);
+   ck_assert_int_ge(pdd.prepared[2], 1);
+
+   ecore_main_fd_handler_del(pdd.fdh[0]);
+   ecore_main_fd_handler_del(pdd.fdh[2]);
+   for (i = 0; i < 3; i++)
+     {
+        close(comm[i][0]);
+        close(comm[i][1]);
+     }
+}
+EFL_END_TEST
+
+typedef struct
+{
+   Ecore_Fd_Handler *fdh[4];
+   int               prepared[4];
+   int               fd;
+   int               depth;
+   Eina_Bool         nested_done;
+} Prepare_Nested_Data;
+
+static Eina_Bool
+_prepare_nested_read_cb(void *data EINA_UNUSED,
+                        Ecore_Fd_Handler *fdh EINA_UNUSED)
+{
+   return ECORE_CALLBACK_RENEW;
+}
+
+static void
+_prepare_nested_cb(void *data, Ecore_Fd_Handler *fdh)
+{
+   Prepare_Nested_Data *pnd = data;
+   int i;
+
+   for (i = 0; i < 4; i++)
+     if (pnd->fdh[i] == fdh) pnd->prepared[i]++;
+
+   if ((fdh == pnd->fdh[0]) && (!pnd->depth) && (!pnd->nested_done))
+     {
+        pnd->depth++;
+        ecore_main_fd_handler_del(pnd->fdh[1]);
+        pnd->fdh[1] = NULL;
+        ecore_main_fd_handler_prepare_callback_set(pnd->fdh[2], NULL, NULL);
+        ecore_main_fd_handler_prepare_callback_set
+          (pnd->fdh[2], _prepare_nested_cb, pnd);
+        pnd->fdh[3] = ecore_main_fd_handler_add
+          (pnd->fd, ECORE_FD_READ, _prepare_nested_read_cb, NULL, NULL, NULL);
+        ck_assert_ptr_nonnull(pnd->fdh[3]);
+        ecore_main_fd_handler_prepare_callback_set
+          (pnd->fdh[3], _prepare_nested_cb, pnd);
+        ecore_main_loop_iterate();
+        pnd->depth--;
+        pnd->nested_done = EINA_TRUE;
+     }
+   else if ((fdh == pnd->fdh[2]) && (!pnd->depth) && (pnd->nested_done))
+     ecore_main_loop_quit();
+}
+
+// The first prepare callback, in the middle of the walk, deletes the next
+// handler, clears and restores the callback of the one after, adds a new
+// handler and runs a nested iteration, so a nested walk and its cleanup run
+// while the outer walk holds its nodes. The readable pipe keeps the loop
+// from sleeping.
+EFL_START_TEST(ecore_test_ecore_main_loop_fd_handler_prepare_nested)
+{
+   Prepare_Nested_Data pnd = { { NULL }, { 0 }, -1, 0, EINA_FALSE };
+   int comm[4][2];
+   int i;
+
+   for (i = 0; i < 4; i++)
+     fail_if(pipe(comm[i]) != 0);
+   pnd.fd = comm[3][0];
+   for (i = 0; i < 3; i++)
+     {
+        pnd.fdh[i] = ecore_main_fd_handler_add
+          (comm[i][0], ECORE_FD_READ, _prepare_nested_read_cb, NULL,
+           NULL, NULL);
+        ck_assert_ptr_nonnull(pnd.fdh[i]);
+        ecore_main_fd_handler_prepare_callback_set
+          (pnd.fdh[i], _prepare_nested_cb, &pnd);
+     }
+   fail_if(write(comm[2][1], "x", 1) != 1);
+
+   ecore_main_loop_begin();
+
+   ck_assert(pnd.nested_done);
+   ck_assert_int_ge(pnd.prepared[0], 2);
+   ck_assert_int_eq(pnd.prepared[1], 0);
+   ck_assert_int_ge(pnd.prepared[2], 2);
+   ck_assert_int_ge(pnd.prepared[3], 2);
+
+   ecore_main_fd_handler_del(pnd.fdh[0]);
+   ecore_main_fd_handler_del(pnd.fdh[2]);
+   ecore_main_fd_handler_del(pnd.fdh[3]);
+   for (i = 0; i < 4; i++)
+     {
+        close(comm[i][0]);
+        close(comm[i][1]);
+     }
+}
+EFL_END_TEST
+
 static Eina_Bool
 _event_handler_cb(void *data, int type, void *event)
 {
@@ -488,6 +693,9 @@ void ecore_test_ecore(TCase *tc)
    tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler);
    tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler_valid_flags);
    tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler_activate_modify);
+   tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler_prepare_null_del);
+   tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler_prepare_del_next);
+   tcase_add_test(tc, ecore_test_ecore_main_loop_fd_handler_prepare_nested);
    tcase_add_test(tc, ecore_test_ecore_main_loop_event);
 #if 0
    tcase_add_test(tc, ecore_test_ecore_main_loop_timer_inner);

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

Reply via email to