For 16/32 bit IDIV, dividing the minimum double-width signed dividend by -1 overflows host division before the quotient check. On x86 hosts this terminates QEMU with SIGFPE.
Detect these cases before dividing and raise the guest divide-error exception. Add a shared i386/x86_64 regression test checking SIGFPE with FPE_INTDIV. Based on the diagnosis by naehrwert in issue #4687. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4687 Signed-off-by: Artemii Mashanov <[email protected]> --- target/i386/tcg/int_helper.c | 4 ++-- tests/tcg/i386/idiv-overflow.c | 42 ++++++++++++++++++++++++++++++++++ tests/tcg/i386/meson.build | 1 + tests/tcg/x86_64/meson.build | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/i386/idiv-overflow.c diff --git a/target/i386/tcg/int_helper.c b/target/i386/tcg/int_helper.c index 772e6ad..d4fa891 100644 --- a/target/i386/tcg/int_helper.c +++ b/target/i386/tcg/int_helper.c @@ -91,7 +91,7 @@ void HELPER(idivw_AX)(CPUX86State *env, target_ulong t0) num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16); den = (int16_t)t0; - if (den == 0) { + if (den == 0 || (num == INT32_MIN && den == -1)) { raise_exception_ra(env, EXCP00_DIVZ, GETPC()); } q = (num / den); @@ -130,7 +130,7 @@ void HELPER(idivl_EAX)(CPUX86State *env, target_ulong t0) num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32); den = t0; - if (den == 0) { + if (den == 0 || (num == INT64_MIN && den == -1)) { raise_exception_ra(env, EXCP00_DIVZ, GETPC()); } q = (num / den); diff --git a/tests/tcg/i386/idiv-overflow.c b/tests/tcg/i386/idiv-overflow.c new file mode 100644 index 0000000..09650a6 --- /dev/null +++ b/tests/tcg/i386/idiv-overflow.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <signal.h> +#include <sys/wait.h> +#include <unistd.h> + +static void on_fpe(int sig, siginfo_t *info, void *context) +{ + (void)context; + _exit(sig != SIGFPE || info->si_code != FPE_INTDIV); +} + +int main(void) +{ + struct sigaction act = { .sa_sigaction = on_fpe, .sa_flags = SA_SIGINFO }; + + assert(sigemptyset(&act.sa_mask) == 0); + assert(sigaction(SIGFPE, &act, NULL) == 0); + for (int bits = 16; bits <= 32; bits += 16) { + pid_t child = fork(); + int status; + + assert(child >= 0); + if (child == 0) { + unsigned int low = 0; + unsigned int high = bits == 16 ? 0x8000 : 0x80000000; + + if (bits == 16) { + asm volatile("idivw %%cx" : "+a"(low), "+d"(high) + : "c"(-1) : "cc"); + } else { + asm volatile("idivl %%ecx" : "+a"(low), "+d"(high) + : "c"(-1) : "cc"); + } + _exit(1); + } + assert(waitpid(child, &status, 0) == child); + assert(WIFEXITED(status) && WEXITSTATUS(status) == 0); + } + return 0; +} diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build index e7c3095..f1b3ac5 100644 --- a/tests/tcg/i386/meson.build +++ b/tests/tcg/i386/meson.build @@ -8,6 +8,7 @@ tests += { 'hello-i386.c': { 'cflags': ['-ffreestanding', '-fno-stack-protector', '-nostdlib'], }, + 'idiv-overflow.c': {}, 'test-aes.c': {'cflags': ['-O1', '-msse2', '-maes']}, 'test-i386-adcox.c': {'cflags': ['-O2']}, 'test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, diff --git a/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build index ad4d1f5..713dde6 100644 --- a/tests/tcg/x86_64/meson.build +++ b/tests/tcg/x86_64/meson.build @@ -25,6 +25,7 @@ test_mmx_h = custom_target('test_mmx_h', output: 'test-mmx.h') tests += { + '../i386/idiv-overflow.c': {}, '../i386/test-i386-ssse3.c': {}, '../i386/test-3dnow.c': { 'cflags': ['-I', meson.current_build_dir(), -- 2.56.0
