diff mbox series

iothread: fix crash with invalid properties

Message ID 20180821100716.13803-1-marcandre.lureau@redhat.com
State New
Headers show
Series iothread: fix crash with invalid properties | expand

Commit Message

Marc-André Lureau Aug. 21, 2018, 10:07 a.m. UTC
-object iothread,id=foo,? will crash qemu:

qemu-system-x86_64:qemu-thread-posix.c:128: qemu_cond_destroy: Assertion `cond->initialized' failed.

Use thread_id != -1 to check if iothread_complete() finished
successfully and the mutex/cond have been initialized.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 iothread.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Marc-André Lureau Aug. 28, 2018, 10:58 a.m. UTC | #1
ping
On Tue, Aug 21, 2018 at 12:07 PM Marc-André Lureau
<marcandre.lureau@redhat.com> wrote:
>
> -object iothread,id=foo,? will crash qemu:
>
> qemu-system-x86_64:qemu-thread-posix.c:128: qemu_cond_destroy: Assertion `cond->initialized' failed.
>
> Use thread_id != -1 to check if iothread_complete() finished
> successfully and the mutex/cond have been initialized.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  iothread.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/iothread.c b/iothread.c
> index aff1281257..2fb1cdf55d 100644
> --- a/iothread.c
> +++ b/iothread.c
> @@ -110,6 +110,7 @@ static void iothread_instance_init(Object *obj)
>      IOThread *iothread = IOTHREAD(obj);
>
>      iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
> +    iothread->thread_id = -1;
>  }
>
>  static void iothread_instance_finalize(Object *obj)
> @@ -117,6 +118,11 @@ static void iothread_instance_finalize(Object *obj)
>      IOThread *iothread = IOTHREAD(obj);
>
>      iothread_stop(iothread);
> +
> +    if (iothread->thread_id != -1) {
> +        qemu_cond_destroy(&iothread->init_done_cond);
> +        qemu_mutex_destroy(&iothread->init_done_lock);
> +    }
>      /*
>       * Before glib2 2.33.10, there is a glib2 bug that GSource context
>       * pointer may not be cleared even if the context has already been
> @@ -135,8 +141,6 @@ static void iothread_instance_finalize(Object *obj)
>          g_main_context_unref(iothread->worker_context);
>          iothread->worker_context = NULL;
>      }
> -    qemu_cond_destroy(&iothread->init_done_cond);
> -    qemu_mutex_destroy(&iothread->init_done_lock);
>  }
>
>  static void iothread_complete(UserCreatable *obj, Error **errp)
> @@ -147,7 +151,6 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
>
>      iothread->stopping = false;
>      iothread->running = true;
> -    iothread->thread_id = -1;
>      iothread->ctx = aio_context_new(&local_error);
>      if (!iothread->ctx) {
>          error_propagate(errp, local_error);
> --
> 2.18.0.547.g1d89318c48
>
>
Fam Zheng Sept. 26, 2018, 8:12 a.m. UTC | #2
On Tue, 08/21 12:07, Marc-André Lureau wrote:
> -object iothread,id=foo,? will crash qemu:
> 
> qemu-system-x86_64:qemu-thread-posix.c:128: qemu_cond_destroy: Assertion `cond->initialized' failed.
> 
> Use thread_id != -1 to check if iothread_complete() finished
> successfully and the mutex/cond have been initialized.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  iothread.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/iothread.c b/iothread.c
> index aff1281257..2fb1cdf55d 100644
> --- a/iothread.c
> +++ b/iothread.c
> @@ -110,6 +110,7 @@ static void iothread_instance_init(Object *obj)
>      IOThread *iothread = IOTHREAD(obj);
>  
>      iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
> +    iothread->thread_id = -1;
>  }
>  
>  static void iothread_instance_finalize(Object *obj)
> @@ -117,6 +118,11 @@ static void iothread_instance_finalize(Object *obj)
>      IOThread *iothread = IOTHREAD(obj);
>  
>      iothread_stop(iothread);
> +
> +    if (iothread->thread_id != -1) {
> +        qemu_cond_destroy(&iothread->init_done_cond);
> +        qemu_mutex_destroy(&iothread->init_done_lock);
> +    }
>      /*
>       * Before glib2 2.33.10, there is a glib2 bug that GSource context
>       * pointer may not be cleared even if the context has already been
> @@ -135,8 +141,6 @@ static void iothread_instance_finalize(Object *obj)
>          g_main_context_unref(iothread->worker_context);
>          iothread->worker_context = NULL;
>      }
> -    qemu_cond_destroy(&iothread->init_done_cond);
> -    qemu_mutex_destroy(&iothread->init_done_lock);

Is there a reason for the reordering against aio_context_unref() and
g_main_context_unref()? The patch otherwise looks good to me.

Fam

>  }
>  
>  static void iothread_complete(UserCreatable *obj, Error **errp)
> @@ -147,7 +151,6 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
>  
>      iothread->stopping = false;
>      iothread->running = true;
> -    iothread->thread_id = -1;
>      iothread->ctx = aio_context_new(&local_error);
>      if (!iothread->ctx) {
>          error_propagate(errp, local_error);
> -- 
> 2.18.0.547.g1d89318c48
> 
>
Marc-André Lureau Sept. 26, 2018, 8:33 a.m. UTC | #3
Hi

On Wed, Sep 26, 2018 at 12:13 PM Fam Zheng <famz@redhat.com> wrote:
>
> On Tue, 08/21 12:07, Marc-André Lureau wrote:
> > -object iothread,id=foo,? will crash qemu:
> >
> > qemu-system-x86_64:qemu-thread-posix.c:128: qemu_cond_destroy: Assertion `cond->initialized' failed.
> >
> > Use thread_id != -1 to check if iothread_complete() finished
> > successfully and the mutex/cond have been initialized.
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >  iothread.c | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/iothread.c b/iothread.c
> > index aff1281257..2fb1cdf55d 100644
> > --- a/iothread.c
> > +++ b/iothread.c
> > @@ -110,6 +110,7 @@ static void iothread_instance_init(Object *obj)
> >      IOThread *iothread = IOTHREAD(obj);
> >
> >      iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
> > +    iothread->thread_id = -1;
> >  }
> >
> >  static void iothread_instance_finalize(Object *obj)
> > @@ -117,6 +118,11 @@ static void iothread_instance_finalize(Object *obj)
> >      IOThread *iothread = IOTHREAD(obj);
> >
> >      iothread_stop(iothread);
> > +
> > +    if (iothread->thread_id != -1) {
> > +        qemu_cond_destroy(&iothread->init_done_cond);
> > +        qemu_mutex_destroy(&iothread->init_done_lock);
> > +    }
> >      /*
> >       * Before glib2 2.33.10, there is a glib2 bug that GSource context
> >       * pointer may not be cleared even if the context has already been
> > @@ -135,8 +141,6 @@ static void iothread_instance_finalize(Object *obj)
> >          g_main_context_unref(iothread->worker_context);
> >          iothread->worker_context = NULL;
> >      }
> > -    qemu_cond_destroy(&iothread->init_done_cond);
> > -    qemu_mutex_destroy(&iothread->init_done_lock);
>
> Is there a reason for the reordering against aio_context_unref() and
> g_main_context_unref()? The patch otherwise looks good to me.

Not that I remember,

Feel free to move on commit, or I can resend.

Thanks

>
> Fam
>
> >  }
> >
> >  static void iothread_complete(UserCreatable *obj, Error **errp)
> > @@ -147,7 +151,6 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
> >
> >      iothread->stopping = false;
> >      iothread->running = true;
> > -    iothread->thread_id = -1;
> >      iothread->ctx = aio_context_new(&local_error);
> >      if (!iothread->ctx) {
> >          error_propagate(errp, local_error);
> > --
> > 2.18.0.547.g1d89318c48
> >
> >
>
Fam Zheng Sept. 26, 2018, 9:12 a.m. UTC | #4
On Wed, 09/26 12:33, Marc-André Lureau wrote:
> Hi
> 
> On Wed, Sep 26, 2018 at 12:13 PM Fam Zheng <famz@redhat.com> wrote:
> >
> > On Tue, 08/21 12:07, Marc-André Lureau wrote:
> > > -object iothread,id=foo,? will crash qemu:
> > >
> > > qemu-system-x86_64:qemu-thread-posix.c:128: qemu_cond_destroy: Assertion `cond->initialized' failed.
> > >
> > > Use thread_id != -1 to check if iothread_complete() finished
> > > successfully and the mutex/cond have been initialized.
> > >
> > > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > > ---
> > >  iothread.c | 9 ++++++---
> > >  1 file changed, 6 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/iothread.c b/iothread.c
> > > index aff1281257..2fb1cdf55d 100644
> > > --- a/iothread.c
> > > +++ b/iothread.c
> > > @@ -110,6 +110,7 @@ static void iothread_instance_init(Object *obj)
> > >      IOThread *iothread = IOTHREAD(obj);
> > >
> > >      iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
> > > +    iothread->thread_id = -1;
> > >  }
> > >
> > >  static void iothread_instance_finalize(Object *obj)
> > > @@ -117,6 +118,11 @@ static void iothread_instance_finalize(Object *obj)
> > >      IOThread *iothread = IOTHREAD(obj);
> > >
> > >      iothread_stop(iothread);
> > > +
> > > +    if (iothread->thread_id != -1) {
> > > +        qemu_cond_destroy(&iothread->init_done_cond);
> > > +        qemu_mutex_destroy(&iothread->init_done_lock);
> > > +    }
> > >      /*
> > >       * Before glib2 2.33.10, there is a glib2 bug that GSource context
> > >       * pointer may not be cleared even if the context has already been
> > > @@ -135,8 +141,6 @@ static void iothread_instance_finalize(Object *obj)
> > >          g_main_context_unref(iothread->worker_context);
> > >          iothread->worker_context = NULL;
> > >      }
> > > -    qemu_cond_destroy(&iothread->init_done_cond);
> > > -    qemu_mutex_destroy(&iothread->init_done_lock);
> >
> > Is there a reason for the reordering against aio_context_unref() and
> > g_main_context_unref()? The patch otherwise looks good to me.
> 
> Not that I remember,
> 
> Feel free to move on commit, or I can resend.

No problem, queued as is.

Fam

> 
> Thanks
> 
> >
> > Fam
> >
> > >  }
> > >
> > >  static void iothread_complete(UserCreatable *obj, Error **errp)
> > > @@ -147,7 +151,6 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
> > >
> > >      iothread->stopping = false;
> > >      iothread->running = true;
> > > -    iothread->thread_id = -1;
> > >      iothread->ctx = aio_context_new(&local_error);
> > >      if (!iothread->ctx) {
> > >          error_propagate(errp, local_error);
> > > --
> > > 2.18.0.547.g1d89318c48
> > >
> > >
> >
> 
> 
> -- 
> Marc-André Lureau
diff mbox series

Patch

diff --git a/iothread.c b/iothread.c
index aff1281257..2fb1cdf55d 100644
--- a/iothread.c
+++ b/iothread.c
@@ -110,6 +110,7 @@  static void iothread_instance_init(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
+    iothread->thread_id = -1;
 }
 
 static void iothread_instance_finalize(Object *obj)
@@ -117,6 +118,11 @@  static void iothread_instance_finalize(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread_stop(iothread);
+
+    if (iothread->thread_id != -1) {
+        qemu_cond_destroy(&iothread->init_done_cond);
+        qemu_mutex_destroy(&iothread->init_done_lock);
+    }
     /*
      * Before glib2 2.33.10, there is a glib2 bug that GSource context
      * pointer may not be cleared even if the context has already been
@@ -135,8 +141,6 @@  static void iothread_instance_finalize(Object *obj)
         g_main_context_unref(iothread->worker_context);
         iothread->worker_context = NULL;
     }
-    qemu_cond_destroy(&iothread->init_done_cond);
-    qemu_mutex_destroy(&iothread->init_done_lock);
 }
 
 static void iothread_complete(UserCreatable *obj, Error **errp)
@@ -147,7 +151,6 @@  static void iothread_complete(UserCreatable *obj, Error **errp)
 
     iothread->stopping = false;
     iothread->running = true;
-    iothread->thread_id = -1;
     iothread->ctx = aio_context_new(&local_error);
     if (!iothread->ctx) {
         error_propagate(errp, local_error);