qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paul Brook <paul@nowt.org>
Subject: [PULL 04/18] i386: pcmpestr 64-bit sign extension bug
Date: Thu, 28 Apr 2022 08:53:21 +0200	[thread overview]
Message-ID: <20220428065335.189795-5-pbonzini@redhat.com> (raw)
In-Reply-To: <20220428065335.189795-1-pbonzini@redhat.com>

From: Paul Brook <paul@nowt.org>

The abs1 function in ops_sse.h only works sorrectly when the result fits
in a signed int. This is fine most of the time because we're only dealing
with byte sized values.

However pcmp_elen helper function uses abs1 to calculate the absolute value
of a cpu register. This incorrectly truncates to 32 bits, and will give
the wrong anser for the most negative value.

Fix by open coding the saturation check before taking the absolute value.

Signed-off-by: Paul Brook <paul@nowt.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 target/i386/ops_sse.h | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/target/i386/ops_sse.h b/target/i386/ops_sse.h
index e4d74b814a..535440f882 100644
--- a/target/i386/ops_sse.h
+++ b/target/i386/ops_sse.h
@@ -2011,25 +2011,23 @@ SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ)
 
 static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl)
 {
-    int val;
+    target_long val, limit;
 
     /* Presence of REX.W is indicated by a bit higher than 7 set */
     if (ctrl >> 8) {
-        val = abs1((int64_t)env->regs[reg]);
+        val = (target_long)env->regs[reg];
     } else {
-        val = abs1((int32_t)env->regs[reg]);
+        val = (int32_t)env->regs[reg];
     }
-
     if (ctrl & 1) {
-        if (val > 8) {
-            return 8;
-        }
+        limit = 8;
     } else {
-        if (val > 16) {
-            return 16;
-        }
+        limit = 16;
     }
-    return val;
+    if ((val > limit) || (val < -limit)) {
+        return limit;
+    }
+    return abs1(val);
 }
 
 static inline int pcmp_ilen(Reg *r, uint8_t ctrl)
-- 
2.35.1




  parent reply	other threads:[~2022-04-28  7:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28  6:53 [PULL 00/18] Misc QEMU patches for 2022-04-28 Paolo Bonzini
2022-04-28  6:53 ` [PULL 01/18] error-report: fix g_date_time_format assertion Paolo Bonzini
2022-04-28  6:53 ` [PULL 02/18] configure: make fortify_source=yes by default Paolo Bonzini
2022-04-28  6:53 ` [PULL 03/18] remove -writeconfig Paolo Bonzini
2022-04-28  6:53 ` Paolo Bonzini [this message]
2022-04-28  6:53 ` [PULL 05/18] meson: show final set of compiler flags Paolo Bonzini
2022-04-28  6:53 ` [PULL 06/18] configure: remove dead code Paolo Bonzini
2022-04-28  6:53 ` [PULL 07/18] qga: wixl: get path to sysroot from pkg-config as intended Paolo Bonzini
2022-04-28  6:53 ` [PULL 08/18] configure: pc-bios/qemu-icon.bmp does not exist Paolo Bonzini
2022-04-28  6:53 ` [PULL 09/18] configure: gcov should not exclude fortify-source Paolo Bonzini
2022-04-28  6:53 ` [PULL 10/18] configure: move --enable/--disable-debug-info to second option parsing pass Paolo Bonzini
2022-04-28  6:53 ` [PULL 11/18] configure, meson: move OpenGL check to meson Paolo Bonzini
2022-04-28  6:53 ` [PULL 12/18] meson, configure: move RDMA options " Paolo Bonzini
2022-04-28  6:53 ` [PULL 13/18] meson, configure: move keyctl test " Paolo Bonzini
2022-04-28  6:53 ` [PULL 14/18] meson, configure: move usbfs " Paolo Bonzini
2022-04-28  6:53 ` [PULL 15/18] meson, configure: move libgio " Paolo Bonzini
2022-04-28  6:53 ` [PULL 16/18] meson: remove CONFIG_XEN_PCI_PASSTHROUGH from config-target.h Paolo Bonzini
2022-04-28  6:53 ` [PULL 17/18] meson, configure: move --enable-module-upgrades to meson Paolo Bonzini
2022-04-28  6:53 ` [PULL 18/18] qemu-options: Limit the -enable-kvm option to the targets that support it Paolo Bonzini
2022-04-28 15:33 ` [PULL 00/18] Misc QEMU patches for 2022-04-28 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=20220428065335.189795-5-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=paul@nowt.org \
    --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).