From: Christian Schoenebeck <qemu_oss@crudebyte.com>
To: qemu-devel@nongnu.org
Cc: Geoffrey McRae <geoff@hostfission.com>, kraxel@redhat.com
Subject: Re: [PATCH] audio/jack: fix use after free segfault
Date: Thu, 20 Aug 2020 15:14:56 +0200 [thread overview]
Message-ID: <9865109.K43dqiA3jv@silver> (raw)
In-Reply-To: <dc6f0fa4785e21fbe0c9a9f82793b5ed@hostfission.com>
On Mittwoch, 19. August 2020 17:57:35 CEST Geoffrey McRae wrote:
> > The ringbuffer implementation looks a bit wild:
> >
> > /* read PCM interleaved */
> > static int qjack_buffer_read(QJackBuffer *buffer, float *dest, int
> > size)
> > {
> >
> > assert(buffer->data);
> > const int samples = size / sizeof(float);
> > int frames = samples / buffer->channels;
> > const int avail = atomic_load_acquire(&buffer->used);
> >
> > if (frames > avail) {
> >
> > frames = avail;
> >
> > }
> >
> > int copy = frames;
> > int rptr = buffer->rptr;
> >
> > while (copy) {
> >
> > for (int c = 0; c < buffer->channels; ++c) {
> >
> > *dest++ = buffer->data[c][rptr];
> >
> > }
> >
> > if (++rptr == buffer->frames) {
> >
> > rptr = 0;
> >
> > }
> >
> > --copy;
> >
> > }
> >
> > buffer->rptr = rptr;
> >
> > atomic_sub(&buffer->used, frames);
> > return frames * buffer->channels * sizeof(float);
> >
> > }
> >
> > On both sides there is no check whether one side is over/underrunning
> > the
> > other side (rptr vs. wptr). I would really recommend using an existing
> > ringbuffer implementation instead of writing one by yourself.
>
> `buffer->used` ensures there is no overwrite unless I have missed
> something?
Right, I missed that you are using that separate variable for that. OK then!
Typical ringbuffer implementations only have a rptr and wptr. That's why I
missed it.
>
> > And question:
> >
> > static size_t qjack_write(HWVoiceOut *hw, void *buf, size_t len)
> > {
> >
> > QJackOut *jo = (QJackOut *)hw;
> > ++jo->c.packets;
> >
> > if (jo->c.state != QJACK_STATE_RUNNING) {
> >
> > qjack_client_recover(&jo->c);
> > return len;
> >
> > }
> >
> > qjack_client_connect_ports(&jo->c);
> > return qjack_buffer_write(&jo->c.fifo, buf, len);
> >
> > }
> >
> > So you are ensuring to reconnect the JACK ports in every cycle. Isn't
> > that a
> > bit often?
>
> No, please see the implementation of qjack_client_connect_ports.
Ah, you mean this entry check:
static void qjack_client_connect_ports(QJackClient *c)
{
if (!c->connect_ports || !c->opt->connect_ports) {
return;
}
...
It's okay. However on the long-term I would consider moving that away from the
audio thread as most JACK API functions are not deterministic, i.e. they could
lead to audio dropouts if executed on audio thread.
Best regards,
Christian Schoenebeck
next prev parent reply other threads:[~2020-08-20 13:16 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-18 12:40 [PATCH] audio/jack: fix use after free segfault Geoffrey McRae
2020-08-18 13:41 ` no-reply
2020-08-18 18:11 ` Christian Schoenebeck
2020-08-18 22:20 ` Geoffrey McRae
2020-08-19 11:30 ` Christian Schoenebeck
2020-08-19 11:45 ` Geoffrey McRae
2020-08-19 12:41 ` Christian Schoenebeck
2020-08-19 12:51 ` Geoffrey McRae
2020-08-19 15:51 ` Christian Schoenebeck
2020-08-19 15:57 ` Geoffrey McRae
2020-08-20 13:14 ` Christian Schoenebeck [this message]
2020-08-19 13:30 ` Gerd Hoffmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9865109.K43dqiA3jv@silver \
--to=qemu_oss@crudebyte.com \
--cc=geoff@hostfission.com \
--cc=kraxel@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).