Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Max Filippov <jcmvbkbc@gmail.com>
To: Alan Modra <amodra@gmail.com>
Cc: binutils@sourceware.org, gdb-patches@sourceware.org
Subject: Re: xtensa_insnbuf_from_chars buffer overflow checking
Date: Tue, 3 Nov 2026 22:38:01 -0800	[thread overview]
Message-ID: <CAMo8BfLsaYtf5Wu9aviWwo5-9af3Hu21NRrMOmeTe=8yXWkdVQ@mail.gmail.com> (raw)
In-Reply-To: <aupodzXtjwzM7n4Z@squeak.grove.modra.org>

On Tue, Nov 3, 2026 at 3:04 PM Alan Modra <amodra@gmail.com> wrote:
> On Tue, Nov 03, 2026 at 05:22:26AM -0800, Max Filippov wrote:
> > On Tue, Nov 3, 2026 at 2:46 AM Alan Modra <amodra@gmail.com> wrote:
> > > +/* Wrapper for xtensa_insnbuf_from_chars to convert frag bytes
> > > +   to xtensa_insnbuf.  */
> > > +
> > > +static bool
> > > +xtensa_insnbuf_from_frag (xtensa_isa isa, xtensa_insnbuf insnbuf,
> > > +                         const fragS *frag, size_t off)
> > > +{
> > > +  const unsigned char *buf = (const unsigned char *) frag->fr_literal + off;
> > > +  size_t len = frag->fr_fix - off;
> > > +  gas_assert (frag->fr_fix > off);
> > > +  bool ret = xtensa_insnbuf_from_chars (isa, insnbuf, buf, len);
> > > +  gas_assert (ret);
> >
> > This assertion is triggered when assembling the attached source with
> > option --text-section-literals with the following backtrace:
>
> Wrong testcase attached?  I don't reproduce the assertion here.

Um, the test case is correct, but I run it with custom xtensa configuration,
the one built from the following overlay:
  https://github.com/jcmvbkbc/xtensa-toolchain-build/blob/master/overlays/original/DE_233L_FPU.tar.gz
with the xtensa-dynconfig generator:
  https://github.com/jcmvbkbc/xtensa-dynconfig/tree/original

In any case, I looked at what's happening and I see that
xtensa_insnbuf_from_frag() is called for a frag that contains
literals and  the instruction length decoding call inside the
xtensa_insnbuf_from_chars() returns an error.
I don't see a reason to assert that xtensa_insnbuf_from_chars()
always succeeds, the callers of xtensa_insnbuf_from_frag()
may need to check the return value.

> > > diff --git a/include/xtensa-isa.h b/include/xtensa-isa.h
> > > index 5c25f122d29..4818559ff94 100644
> > > --- a/include/xtensa-isa.h
> > > +++ b/include/xtensa-isa.h
> > > @@ -210,7 +210,7 @@ extern int
> > >  xtensa_insnbuf_to_chars (xtensa_isa isa, const xtensa_insnbuf insn,
> > >                          unsigned char *cp, int num_chars);
> > >
> > > -extern void
> > > +extern bool
> >
> > Other similar functions in this library have return type `int` and return
> > 0 on success and a negative error code on failure. Could this function
> > please follow this pattern too? That's because this header is included
> > into the generated configuration-specific xtensa-modules.c and does
> > not include <stdbool.h> itself.
>
> I can do that for consistency, but I can't say I like the practice of
> avoiding bool.

Neither do I. Is there a reason not to add `#include <stdbool.h>`
to the include/xtensa-isa.h ?

>  Especially for functions like xtensa_opcode_is_branch
> which are used everywhere testing == 1 or > 0 except one place in
> xtensa-dis.c.  (Maybe a bug?)

It would be a bug if it was possible to get there with an invalid opcode,
but according to the code above it that sets and checks `valid_insn`
it seems impossible.

-- 
Thanks.
-- Max

  reply	other threads:[~2026-11-04  6:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 10:46 Alan Modra
2026-11-03 13:22 ` Max Filippov
2026-11-03 23:04   ` Alan Modra
2026-11-04  6:38     ` Max Filippov [this message]
2026-11-09  4:20       ` Alan Modra
2026-11-09 14:04         ` Max Filippov

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='CAMo8BfLsaYtf5Wu9aviWwo5-9af3Hu21NRrMOmeTe=8yXWkdVQ@mail.gmail.com' \
    --to=jcmvbkbc@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.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