qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
Cc: Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	QEMU Developers <qemu-devel@nongnu.org>,
	Aurelien Jarno <aurelien@aurel32.net>,
	Craig Janeczek <jancraig@amazon.com>
Subject: Re: [PATCH] target/mips/mxu: Rewrite D16MIN / D16MAX opcodes
Date: Tue, 16 Mar 2021 10:51:08 +0000	[thread overview]
Message-ID: <CAFEAcA-+m-eADtYini-2Mscom-RiuvzG4cXndMcuSKR80O8dow@mail.gmail.com> (raw)
In-Reply-To: <20210315223745.2953548-1-f4bug@amsat.org>

On Mon, 15 Mar 2021 at 22:39, Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>
> Coverity reported (CID 1450831) an array overrun in
> gen_mxu_D16MAX_D16MIN():
>
>   1103     } else if (unlikely((XRb == 0) || (XRa == 0))) {
>   ....
>   1112         if (opc == OPC_MXU_D16MAX) {
>   1113             tcg_gen_smax_i32(mxu_gpr[XRa - 1], t0, t1);
>   1114         } else {
>   1115             tcg_gen_smin_i32(mxu_gpr[XRa - 1], t0, t1);
>   1116         }
>
> >>> Overrunning array "mxu_gpr" of 15 8-byte elements at element
>     index 4294967295 (byte offset 34359738367) using index "XRa - 1U"
>     (which evaluates to 4294967295).
>
> Because we check if 'XRa == 0' then access 'XRa - 1' in array.
>
> I figured it could be easier to rewrite this function to something
> simpler rather than trying to understand it.

This seems to drop half of the optimised cases the old
code had. Wouldn't it be simpler just to fix the bugs
in the conditions?

That is:

>      if (unlikely(pad != 0)) {
>          /* opcode padding incorrect -> do nothing */
> -    } else if (unlikely(XRc == 0)) {
> -        /* destination is zero register -> do nothing */

This should be "XRa == 0"

> -    } else if (unlikely((XRb == 0) && (XRa == 0))) {
> -        /* both operands zero registers -> just set destination to zero */

This should be "XRb == 0 && XRc == 0"

> -        tcg_gen_movi_i32(mxu_gpr[XRc - 1], 0);

This should set mxu_gpr[XRa - 1]

> -    } else if (unlikely((XRb == 0) || (XRa == 0))) {

This should be "XRb == 0 || XRc == 0"

And everything else in the function looks OK to me.

thanks
-- PMM


  reply	other threads:[~2021-03-16 10:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 22:37 [PATCH] target/mips/mxu: Rewrite D16MIN / D16MAX opcodes Philippe Mathieu-Daudé
2021-03-16 10:51 ` Peter Maydell [this message]
2021-03-16 12:03   ` Philippe Mathieu-Daudé
2021-03-16 15:27     ` Philippe Mathieu-Daudé
2021-03-16 15:21   ` Richard Henderson

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=CAFEAcA-+m-eADtYini-2Mscom-RiuvzG4cXndMcuSKR80O8dow@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=aleksandar.rikalo@syrmia.com \
    --cc=aurelien@aurel32.net \
    --cc=f4bug@amsat.org \
    --cc=jancraig@amazon.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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).