The first immediate operand of ENTER is the size of the dynamic storage area and is unsigned. Decoding it as a signed immediate sign-extends values with bit 15 set, moving the stack pointer in the wrong direction.
Decode it with I_unsigned and add a regression test covering 32-bit and 64-bit guests. Tested with a minimal x86_64-linux-user build; the new test fails before the change and passes after it. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4643 Signed-off-by: Zixin Wang <[email protected]> --- target/i386/tcg/decode-new.c.inc | 2 +- tests/tcg/i386/meson.build | 1 + tests/tcg/i386/test-i386-enter.c | 42 ++++++++++++++++++++++++++++++++ tests/tcg/x86_64/meson.build | 1 + 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/i386/test-i386-enter.c diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc index aa22014..c0ceefb 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -1968,7 +1968,7 @@ static const X86OpEntry opcodes_root[256] = { [0xBE] = X86_OP_ENTRYwr(MOV, LoBits,v, I,v), [0xBF] = X86_OP_ENTRYwr(MOV, LoBits,v, I,v), - [0xC8] = X86_OP_ENTRYrr(ENTER, I,w, I,b), + [0xC8] = X86_OP_ENTRYrr(ENTER, I_unsigned, w, I, b), [0xC9] = X86_OP_ENTRY0(LEAVE), [0xCA] = X86_OP_ENTRYr(RETF, I,w), [0xCB] = X86_OP_ENTRY0(RETF), diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build index 648b516..ebe2e6a 100644 --- a/tests/tcg/i386/meson.build +++ b/tests/tcg/i386/meson.build @@ -36,6 +36,7 @@ tests += { '-lm', '-fno-pie', '-Wl,--no-warn-rwx-segments'], }, 'test-idiv-overflow.c': {}, + 'test-i386-enter.c': {'cflags': ['-O2', '-fomit-frame-pointer']}, '../multiarch/sha512.c': { 'exe_name': 'sha512-sse', 'cflags': ['-msse4.1', '-O3'], diff --git a/tests/tcg/i386/test-i386-enter.c b/tests/tcg/i386/test-i386-enter.c new file mode 100644 index 0000000..509740d --- /dev/null +++ b/tests/tcg/i386/test-i386-enter.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * ENTER's 16-bit size operand is unsigned. If the decoder sign-extends + * it, the stack pointer moves in the wrong direction. + */ +#include <stdio.h> + +static int check_enter(void) +{ + unsigned long before, after; + +#ifdef __x86_64__ + __asm__ volatile( + "movq %%rsp, %0\n\t" + "enter $0x8000, $0\n\t" + "movq %%rsp, %1\n\t" + "leave\n\t" + : "=r"(before), "=r"(after) + : + : "memory"); + return (before - after) == 0x8008 ? 0 : 1; +#else + __asm__ volatile( + "movl %%esp, %0\n\t" + "enter $0x8000, $0\n\t" + "movl %%esp, %1\n\t" + "leave\n\t" + : "=r"(before), "=r"(after) + : + : "memory"); + return (before - after) == 0x8004 ? 0 : 1; +#endif +} + +int main(void) +{ + if (check_enter()) { + printf("FAIL: ENTER treated the 16-bit size operand as signed\n"); + return 1; + } + return 0; +} diff --git a/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build index fc7f48e..9e34a39 100644 --- a/tests/tcg/x86_64/meson.build +++ b/tests/tcg/x86_64/meson.build @@ -49,6 +49,7 @@ tests += { '../i386/test-i386-adcox.c': {'cflags': ['-O2']}, '../i386/test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, '../i386/test-idiv-overflow.c': {}, + '../i386/test-i386-enter.c': {'cflags': ['-O2', '-fomit-frame-pointer']}, } tests += { -- 2.56.0
