From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wkzsd-0001e1-5I for qemu-devel@nongnu.org; Thu, 15 May 2014 13:55:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WkzsT-0004yq-Qf for qemu-devel@nongnu.org; Thu, 15 May 2014 13:55:43 -0400 Sender: fluxion Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth In-Reply-To: <87zjij0xkj.fsf@blackfin.pond.sub.org> References: <1399473780-20374-1-git-send-email-marcel.a@redhat.com> <1399473780-20374-3-git-send-email-marcel.a@redhat.com> <87lhu3qaui.fsf@blackfin.pond.sub.org> <20140515162704.3192.88046@loki> <87zjij0xkj.fsf@blackfin.pond.sub.org> Message-ID: <20140515175528.3192.41143@loki> Date: Thu, 15 May 2014 12:55:28 -0500 Subject: Re: [Qemu-devel] [PATCH 2/4] qapi: output visitor crashes qemu if it encounters a NULL value List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: mst@redhat.com, aik@ozlabs.ru, qemu-devel@nongnu.org, lcapitulino@redhat.com, blauwirbel@gmail.com, jcmvbkbc@gmail.com, edgar.iglesias@gmail.com, gxt@mprc.pku.edu.cn, proljc@gmail.com, agraf@suse.de, scottwood@freescale.com, borntraeger@de.ibm.com, hpoussin@reactos.org, aliguori@amazon.com, lersek@redhat.com, Marcel Apfelbaum , chouteau@adacore.com, jan.kiszka@web.de, stefanha@redhat.com, pbonzini@redhat.com, cornelia.huck@de.ibm.com, peter.crosthwaite@xilinx.com, mark.langsdorf@calxeda.com, michael@walle.cc, qemu-ppc@nongnu.org, peter.chubb@nicta.com.au, afaerber@suse.de, aurelien@aurel32.net Quoting Markus Armbruster (2014-05-15 12:19:08) > Michael Roth writes: > = > > Quoting Markus Armbruster (2014-05-15 11:13:09) > >> Marcel Apfelbaum writes: > >> = > >> > A NULL value is not added to visitor's stack, but there > >> > is no check for that when the visitor tries to return > >> > that value, leading to Qemu crash. > >> > > >> > Reviewed-by: Eric Blake > >> > Signed-off-by: Marcel Apfelbaum > >> > --- > >> > qapi/qmp-output-visitor.c | 5 +++++ > >> > 1 file changed, 5 insertions(+) > >> > > >> > diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c > >> > index 74a5684..0562f49 100644 > >> > --- a/qapi/qmp-output-visitor.c > >> > +++ b/qapi/qmp-output-visitor.c > >> > @@ -66,6 +66,11 @@ static QObject *qmp_output_pop(QmpOutputVisitor *= qov) > >> > static QObject *qmp_output_first(QmpOutputVisitor *qov) > >> > { > >> > QStackEntry *e =3D QTAILQ_LAST(&qov->stack, QStack); > >> > + > >> > + if (!e) { > >> > + return NULL; > >> > + } > >> > + > >> > return e->value; > >> > } > >> = > >> Let's see how this thing works. > >> = > >> The visitor's mutable state is a QStack, which is stack of (QObject, > >> bool). We can ignore the bool; it's just for qmp_output_next_list(). > >> = > >> Visits start with an empty stack. See qmp_output_visitor_new(). > >> = > >> qmp_output_first() returns the object on the bottom of the stack. > >> qmp_output_last() returns the object on the top of the stack. > >> = > >> > >> When you implement a stack with a double-ended queue, you're totally > >> free to pick either end of the queue for top of stack. You're also fr= ee > >> to name your functions accessing top and the bottom of the stack howev= er > >> you like. "Of course" the author picked queue end and function names > >> for maximum confusion: > >> = > >> static QObject *qmp_output_first(QmpOutputVisitor *qov) > >> { > >> QStackEntry *e =3D QTAILQ_LAST(&qov->stack, QStack); > >> return e->value; > >> } > >> = > >> static QObject *qmp_output_last(QmpOutputVisitor *qov) > >> { > >> QStackEntry *e =3D QTAILQ_FIRST(&qov->stack); > >> return e->value; > >> } > >> = > >> I hate you. > >> > >> = > >> The result of the visit sits at the bottom of the stack. Empty stack, > >> null result. See qmp_output_get_qobject(). > >> = > >> Visiting a scalar type creates the appropriate scalar QObject, and > >> "adds" it. We'll find out what "adding" means shortly. See > >> qmp_output_type_{int,bool,str,number}(). > >> = > >> Special case: null strings get converted to empty strings. See > >> qmp_output_type_str(). > >> = > >> Starting a struct visit creates a QDict, adds it, and pushes it onto t= he > >> stack. Ending it pops it from the stack. See > >> qmp_output_{start,end}_struct(). > >> = > >> Starting a list visit creates a QList, adds it, and pushes it onto the > >> stack. Ending it pops it from the stack. See > >> qmp_output_{start,end}_list(). > >> = > >> Visiting a list member does nothing interesting; see > >> qmp_output_next_list(). Aside: I suspect the GenericList traversal > >> stuff now done in every next_list() method should be done in the visit= or > >> core instead. > >> = > >> Now let's figure out what it means to "add" an object. This is > >> qmp_output_add_obj(). > >> = > >> If the stack is still empty, the object is the root object, and it gets > >> pushed. > >> = > >> Else, if the object on top of the stack is a QDict, we're visiting a > >> struct. Enter the object into the QDict. > >> = > >> Else, if the object on top of the stack is a QList, we're visiting a > >> list. Append the object to the QList. > >> = > >> Else, the object on top of the stack must be scalar, and I think it mu= st > >> be the root object. We replace it by the object being added. WTF? > >> = > >> This feels more complicated than it could be. Anyway, how could a null > >> object end up at the bottom of the stack, so that qmp_output_first() > >> chokes on it? I can't see that. > >> = > >> If it can get added, then why can it be seen only by qmp_output_first(= ), > >> but not by qmp_output_last() and qmp_output_pop()? > > > > See my note above, the corner case we're hitting seems to be when there= 's > > nothing in the stack at all: generating a QObject from an empty > > QmpOutputVisitor. > = > The other user of qmp_output_first() calls it like this: > = > QObject *root =3D QTAILQ_EMPTY(&v->stack) ? NULL : qmp_output_first(v= ); > = > Patching qmp_output_first() makes this check redundant. > = > I suspect we should change both callers to test QTAILQ_EMPTY() instead. Or remove the redundant check, don't have a strong preference either way, though personally I think qmp_output_first should handle it internally and just give us the (possibly NULL) QObject, since it exposes less internals to callers. > = > > This occurs with object_property_get_str skips visit_type_str if the > > property-specific accessor returns NULL, but we still covert the > > visitor to a QObject to pull the string out later. > = > Can't see visit_type_str() being called from object_property_get_str(). > Do you mean property_get_str()? Yah, I think it's something like: object_property_get_str->object_property_get->prop.get->property_get_str > = > static void property_get_str(Object *obj, Visitor *v, void *opaque, > const char *name, Error **errp) > { > StringProperty *prop =3D opaque; > char *value; > = > value =3D prop->get(obj, errp); > if (value) { > visit_type_str(v, &value, name, errp); > g_free(value); > } > } > = > Why do we skip visit_type_str() when value is null? Not sure..., seems like an explicit fall through to the QERR_INVALID_PARAMETER_TYPE error in object_property_get_str that wasn't reachable prior to Marcel's patch (due to segfault), so I'm not sure this code path was in play until now.