Received: from eggs.gnu.org ([2001:4830:134:3::10]:40326) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bqzq9-0000WT-RE for qemu-devel@nongnu.org; Mon, 03 Oct 2016 05:47:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bqzq7-0005p4-LP for qemu-devel@nongnu.org; Mon, 03 Oct 2016 05:47:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33682) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bqzq7-0005oR-BS for qemu-devel@nongnu.org; Mon, 03 Oct 2016 05:47:15 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D008B68822 for ; Mon, 3 Oct 2016 09:47:14 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Mon, 3 Oct 2016 13:47:04 +0400 Message-Id: <20161003094704.18087-3-marcandre.lureau@redhat.com> In-Reply-To: <20161003094704.18087-1-marcandre.lureau@redhat.com> References: <20161003094704.18087-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 2/2] char: use a fixed idx for child muxed chr List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, kraxel@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= mux_chr_update_read_handler() is adding a new mux_cnt each time mux_chr_update_read_handler() is called, it's not possible to actually update the "child" chr callbacks that were set previously. This may lead to crashes if the "child" chr is destroyed: valgrind x86_64-softmmu/qemu-system-x86_64 -chardev stdio,mux=3Don,id=3Dchar0 -mon chardev=3Dchar0,mode=3Dcontrol,default when quitting: =3D=3D4306=3D=3D Invalid read of size 8 =3D=3D4306=3D=3D at 0x8061D3: json_lexer_destroy (json-lexer.c:385) =3D=3D4306=3D=3D by 0x7E39F8: json_message_parser_destroy (json-stream= er.c:134) =3D=3D4306=3D=3D by 0x3447F6: monitor_qmp_event (monitor.c:3908) =3D=3D4306=3D=3D by 0x480153: mux_chr_send_event (qemu-char.c:630) =3D=3D4306=3D=3D by 0x480694: mux_chr_event (qemu-char.c:734) =3D=3D4306=3D=3D by 0x47F1E9: qemu_chr_be_event (qemu-char.c:205) =3D=3D4306=3D=3D by 0x481207: fd_chr_close (qemu-char.c:1114) =3D=3D4306=3D=3D by 0x481659: qemu_chr_close_stdio (qemu-char.c:1221) =3D=3D4306=3D=3D by 0x486F07: qemu_chr_free (qemu-char.c:4146) =3D=3D4306=3D=3D by 0x486F97: qemu_chr_delete (qemu-char.c:4154) =3D=3D4306=3D=3D by 0x487E66: qemu_chr_cleanup (qemu-char.c:4678) =3D=3D4306=3D=3D by 0x495A98: main (vl.c:4675) =3D=3D4306=3D=3D Address 0x28439e90 is 112 bytes inside a block of size = 240 free'd =3D=3D4306=3D=3D at 0x4C2CD5A: free (vg_replace_malloc.c:530) =3D=3D4306=3D=3D by 0x1E4CBF2D: g_free (in /usr/lib64/libglib-2.0.so.0= .4800.2) =3D=3D4306=3D=3D by 0x344DE9: monitor_cleanup (monitor.c:4058) =3D=3D4306=3D=3D by 0x495A93: main (vl.c:4674) =3D=3D4306=3D=3D Block was alloc'd at =3D=3D4306=3D=3D at 0x4C2BBAD: malloc (vg_replace_malloc.c:299) =3D=3D4306=3D=3D by 0x1E4CBE18: g_malloc (in /usr/lib64/libglib-2.0.so= .0.4800.2) =3D=3D4306=3D=3D by 0x344BF8: monitor_init (monitor.c:4021) =3D=3D4306=3D=3D by 0x49063C: mon_init_func (vl.c:2417) =3D=3D4306=3D=3D by 0x7FC6DE: qemu_opts_foreach (qemu-option.c:1116) =3D=3D4306=3D=3D by 0x4954E0: main (vl.c:4473) Instead, keep the "child" chr associated with a particular idx so its handlers can be updated and removed to avoid the crash. Signed-off-by: Marc-Andr=C3=A9 Lureau --- qemu-char.c | 22 +++++++++++++++------- include/sysemu/char.h | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index f7a07e6..4b330ea 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -165,6 +165,7 @@ CharDriverState *qemu_chr_alloc(ChardevCommon *backen= d, Error **errp) CharDriverState *chr =3D g_malloc0(sizeof(CharDriverState)); qemu_mutex_init(&chr->chr_write_lock); =20 + chr->mux_idx =3D -1; if (backend->has_logfile) { int flags =3D O_WRONLY | O_CREAT; if (backend->has_logappend && @@ -738,17 +739,25 @@ static void mux_chr_update_read_handler(CharDriverS= tate *chr, GMainContext *context) { MuxDriver *d =3D chr->opaque; + int idx; =20 if (d->mux_cnt >=3D MAX_MUX) { fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); return; } - d->ext_opaque[d->mux_cnt] =3D chr->handler_opaque; - d->chr_can_read[d->mux_cnt] =3D chr->chr_can_read; - d->chr_read[d->mux_cnt] =3D chr->chr_read; - d->chr_event[d->mux_cnt] =3D chr->chr_event; + + if (chr->mux_idx =3D=3D -1) { + chr->mux_idx =3D d->mux_cnt++; + } + + idx =3D chr->mux_idx; + d->ext_opaque[idx] =3D chr->handler_opaque; + d->chr_can_read[idx] =3D chr->chr_can_read; + d->chr_read[idx] =3D chr->chr_read; + d->chr_event[idx] =3D chr->chr_event; + /* Fix up the real driver with mux routines */ - if (d->mux_cnt =3D=3D 0) { + if (d->mux_cnt =3D=3D 1) { qemu_chr_add_handlers_full(d->drv, mux_chr_can_read, mux_chr_read, mux_chr_event, @@ -757,8 +766,7 @@ static void mux_chr_update_read_handler(CharDriverSta= te *chr, if (d->focus !=3D -1) { mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); } - d->focus =3D d->mux_cnt; - d->mux_cnt++; + d->focus =3D idx; mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); } =20 diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 0d0465a..4593576 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -92,6 +92,7 @@ struct CharDriverState { int explicit_be_open; int avail_connections; int is_mux; + int mux_idx; guint fd_in_tag; QemuOpts *opts; bool replay; --=20 2.10.0