Skip to contentSkip to conversation

target/s390x: Fix DR/D INT64_MIN / -1 host crash

PATCH Applied Ilya Leoshkevich 3 participants 6 messages Jul 14 – Jul 27

lore
Share & tools
lore
#0 lore raw

cover target/s390x: Fix DR/D INT64_MIN / -1 host crash

Hi,

Christian reported that INT64_MIN / -1 crashes the emulator itself instead of the guest program. This series fixes the issue and adds a test.

Best regards,
Ilya

Ilya Leoshkevich (2):
target/s390x: Fix DR/D INT64_MIN / -1 host crash tests/tcg/s390x: Test DR overflow (INT64_MIN / -1)

 target/s390x/tcg/int_helper.c |  3 ++-
 tests/tcg/s390x/div.c         | 37 +++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 target/s390x: Fix DR/D INT64_MIN / -1 host crash

helper_divs32() divides the 64-bit dividend by the 32-bit divisor as a 64-bit host operation, guarding only against a zero divisor. INT64_MIN / -1 therefore overflows the host division before the representability check runs; on hosts that trap this, QEMU is killed with SIGFPE instead of raising the fixed-point-divide exception the guest expects:

    qemu-s390x: QEMU internal SIGFPE {code=INTDIV, addr=...}

helper_divs64() already guards the same case; add the missing check to helper_divs32().

Fixes b4e2bd3563af ("target-s390: Send signals for divide")
---
 target/s390x/tcg/int_helper.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/s390x/tcg/int_helper.c +2 -1
index fbda396f5b4..5aedd1405bf 100644
@@ -39,7 +39,8 @@ uint64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
     int32_t b = b64;
     int64_t q, r;
 
-    if (b == 0) {
+    /* Catch divide by zero, and non-representable quotient (MIN / -1).  */
+    if (b == 0 || (b == -1 && a == (1ll << 63))) {
         tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
     }
 
Signature · 1 line
2.55.0
Reviewed-by on v1 1/2
#2 lore raw
Quoted 11 lines · Ilya Leoshkevich

helper_divs32() divides the 64-bit dividend by the 32-bit divisor as a 64-bit host operation, guarding only against a zero divisor. INT64_MIN / -1 therefore overflows the host division before the representability check runs; on hosts that trap this, QEMU is killed with SIGFPE instead of raising the fixed-point-divide exception the guest expects:

     qemu-s390x: QEMU internal SIGFPE {code=INTDIV, addr=...}

helper_divs64() already guards the same case; add the missing check to helper_divs32().

Quoted 7 lines
Fixes b4e2bd3563af ("target-s390: Send signals for divide")
---
  target/s390x/tcg/int_helper.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

r~

#3 lore raw

PATCH 2/2 tests/tcg/s390x: Test DR overflow (INT64_MIN / -1)

In reply to Ilya Leoshkevich

Check that DR with a non-representable quotient raises SIGFPE rather than crashing the emulator.

---
 tests/tcg/s390x/div.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tests/tcg/s390x/div.c +37 -0
index 6ad9900e082..124c9ecc339 100644
@@ -1,6 +1,15 @@
 #include <assert.h>
+#include <signal.h>
 #include <stdint.h>
 
+/* Set asynchronously by the signal handler. */
+static volatile int signum;
+
+static void signal_handler(int n)
+{
+    signum = n;
+}
+
 static void test_dr(void)
 {
     register int32_t r0 asm("r0") = -1;
@@ -65,11 +74,39 @@ static void test_dlgr(void)
     assert(r == 1);
 }
 
+/*
+ * The most negative dividend divided by -1 yields a quotient that does not
+ * fit into 32 bits, so DR must raise a fixed-point-divide exception.
+ */
+static void test_dr_overflow(void)
+{
+    struct sigaction act = { .sa_handler = signal_handler };
+    register int32_t r0 asm("r0");
+    register int32_t r1 asm("r1");
+    int32_t b = -1;
+    int err;
+
+    err = sigaction(SIGFPE, &act, NULL);
+    assert(err == 0);
+    signum = -1;
+
+    r0 = 0x80000000;
+    r1 = 0;
+    asm volatile("dr %[r0],%[b]"
+                 : [r0] "+r" (r0), [r1] "+r" (r1)
+                 : [b] "r" (b)
+                 : "cc");
+    assert(signum == SIGFPE);
+
+    signal(SIGFPE, SIG_DFL);
+}
+
 int main(void)
 {
     test_dr();
     test_dlr();
     test_dsgr();
     test_dlgr();
+    test_dr_overflow();
     return 0;
 }
Signature · 1 line
2.55.0
Applied
#5 lore raw
In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich

Hi,

Christian reported that INT64_MIN / -1 crashes the emulator itself instead of the guest program. This series fixes the issue and adds a test.

Quoted 6 lines

Best regards,
Ilya

Ilya Leoshkevich (2):

   target/s390x: Fix DR/D INT64_MIN / -1 host crash
   tests/tcg/s390x: Test DR overflow (INT64_MIN / -1)

Thanks! Applied for 11.2

Quoted 5 lines
  target/s390x/tcg/int_helper.c |  3 ++-
  tests/tcg/s390x/div.c         | 37 +++++++++++++++++++++++++++++++++++
  2 files changed, 39 insertions(+), 1 deletion(-)