Fix BRASL and BRCL with large negative offsets
v2 cover Fix BRASL and BRCL with large negative offsets
Hi,
I noticed that sometimes jumping backwards leads to crashes or hangs. The problem is a missing cast.
Patches 1 and 2 fix the problem, patch 3 adds a test.
v1: https://lists.nongnu.org/archive/html/qemu-devel/2022-03/msg03356.html
v1 -> v2:
- Skip the test if mmap() fails (Richard).
- Replace test opcodes with inline asm (David). Since we now want to skip the test if the code cannot be mapped (e.g. on a 31-bit host), we shouldn't be asking the loader to map the code right away. So the mmap() approach stays.
Best regards,
Ilya
Ilya Leoshkevich (3):
s390x/tcg: Fix BRASL with a large negative offset s390x/tcg: Fix BRCL with a large negative offset tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
target/s390x/tcg/translate.c | 4 +- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/branch-relative-long.c | 60 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/s390x/branch-relative-long.c
Signature · 1 line
2.35.1
PATCH v2 1/3 s390x/tcg: Fix BRASL with a large negative offset
When RI2 is 0x80000000, qemu enters an infinite loop instead of jumping backwards. Fix by adding a missing cast, like in in2_ri2().
--- target/s390x/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/tcg/translate.c +1 -1
@@ -1597,7 +1597,7 @@ static DisasJumpType op_bal(DisasContext *s, DisasOps *o)static DisasJumpType op_basi (DisasContext *s,DisasOps *o) {pc_to_link_info (o->out, s, s->pc_tmp); -return help_goto_direct (s, s->base.pc_next +2 *get_field (s, i2)); +return help_goto_direct (s, s->base.pc_next + (int64_t )get_field (s, i2) *2 ); }static DisasJumpType op_bc (DisasContext *s,DisasOps *o)
Signature · 1 line
2.35.1
PATCH v2 2/3 s390x/tcg: Fix BRCL with a large negative offset
In reply to Ilya LeoshkevichWhen RI2 is 0x80000000, qemu enters an infinite loop instead of jumping backwards. Fix by adding a missing cast, like in in2_ri2().
--- target/s390x/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/tcg/translate.c +1 -1
@@ -1201,7 +1201,7 @@ static DisasJumpType help_branch(DisasContext *s, DisasCompare *c, bool is_imm, int imm, TCGv_i64 cdest) { DisasJumpType ret; -uint64_t dest = s->base.pc_next +2 * imm; +uint64_t dest = s->base.pc_next + (int64_t )imm *2 ;TCGLabel *lab;/* Take care of the special cases first. */
Signature · 1 line
2.35.1
PATCH v2 3/3 tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
In reply to Ilya LeoshkevichAdd a small test in order to prevent regressions.
--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/branch-relative-long.c | 60 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/tcg/s390x/branch-relative-long.c
tests/tcg/s390x/Makefile.target +1 -0
@@ -15,6 +15,7 @@ TESTS+=mvc TESTS+=shift TESTS+=trap TESTS+=signals-s390x +TESTS+=branch-relative-long ifneq ($(HAVE_GDB_BIN),) GDB_SCRIPT=$(SRC_PATH)/tests/guest-debug/run-test.py
tests/tcg/s390x/branch-relative-long.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH v2 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
Quoted 54 lines · Ilya Leoshkevich
Add a small test in order to prevent regressions.
Signed-off-by Ilya Leoshkevich <[email protected]>--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/branch-relative-long.c | 60 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+)create mode 100644
tests/tcg/s390x/branch-relative-long.ctests/tcg/s390x/Makefile.target +1
-0@@ -15,6 +15,7 @@ TESTS+=mvc TESTS+=shift TESTS+=trap TESTS+=signals-s390x +TESTS+=branch-relative-long ifneq ($(HAVE_GDB_BIN),) GDB_SCRIPT=$(SRC_PATH)/tests/guest-debug/run-test.pytests/tcg/s390x/branch-relative-long.c new file +27
-0@@ -0,0 +1,60 @@ +#include <stddef.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> + +#define DEFINE_ASM (_name, _code) \ + extern const char _name[]; \ + extern const char _name ## _end[]; \ + asm(" .globl " #_name "\n" \ + #_name ":\n" \ + " " _code "\n" \ + " .globl " #_name "_end\n" \ + #_name "_end:\n"); + +DEFINE_ASM (br_r14,"br %r14" ); +DEFINE_ASM (brasl_r0,"brasl %r0,.-0x100000000" ); +DEFINE_ASM (brcl_0xf,"brcl 0xf,.-0x100000000" ); + +struct test { +const char *code; +const char *code_end; +}; + +static const struct test tests[] = { + { + .code = brasl_r0, + .code_end = brasl_r0_end,
tests/tcg/s390x/branch-relative-long.c line 37
Quoted 10 lines
tests/tcg/s390x/branch-relative-long.c new file +10
-0+ }, + { + .code = brcl_0xf, + .code_end = brcl_0xf_end, + }, +}; + +int main (void ) +{ +size_t length =0x100000006 ;
This length is a little specific, given the apparent open-endedness of "code_end". Though of course we know that we'll get an entire page allocated for these 6 bytes so there's no real problem.
I'm not actually sure what to suggest to improve this though.
For the series:
r~
Quoted 23 lines
tests/tcg/s390x/branch-relative-long.c new file +23
-0+unsigned char *buf; +void (*code)(void ); +size_t i; + + buf =mmap (NULL , length,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (buf ==MAP_FAILED ) { +perror ("SKIP: mmap() failed" ); +return 0 ; + } + +memcpy (buf, br_r14, br_r14_end - br_r14); +for (i =0 ; i <sizeof (tests) /sizeof (tests[0 ]); i++) { + code = (void *)(buf +0x100000000 ); +memcpy (code, tests[i].code, tests[i].code_end - tests[i].code); +code (); +memset (code,0 , tests[i].code_end - tests[i].code); + } + +munmap (buf, length); + +return 0 ; +}
Re: [PATCH v2 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
In reply to Ilya LeoshkevichQuoted 60 lines · Ilya Leoshkevich
Add a small test in order to prevent regressions.
Signed-off-by Ilya Leoshkevich <[email protected]>--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/branch-relative-long.c | 60 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/tcg/s390x/branch-relative-long.ctests/tcg/s390x/Makefile.target +1
-0@@ -15,6 +15,7 @@ TESTS+=mvc TESTS+=shift TESTS+=trap TESTS+=signals-s390x +TESTS+=branch-relative-long ifneq ($(HAVE_GDB_BIN),) GDB_SCRIPT=$(SRC_PATH)/tests/guest-debug/run-test.pytests/tcg/s390x/branch-relative-long.c new file +33
-0@@ -0,0 +1,60 @@ +#include <stddef.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> + +#define DEFINE_ASM (_name, _code) \ + extern const char _name[]; \ + extern const char _name ## _end[]; \ + asm(" .globl " #_name "\n" \ + #_name ":\n" \ + " " _code "\n" \ + " .globl " #_name "_end\n" \ + #_name "_end:\n"); + +DEFINE_ASM (br_r14,"br %r14" ); +DEFINE_ASM (brasl_r0,"brasl %r0,.-0x100000000" ); +DEFINE_ASM (brcl_0xf,"brcl 0xf,.-0x100000000" ); + +struct test { +const char *code; +const char *code_end; +}; + +static const struct test tests[] = { + { + .code = brasl_r0, + .code_end = brasl_r0_end, + }, + { + .code = brcl_0xf, + .code_end = brcl_0xf_end, + }, +};
tests/tcg/s390x/branch-relative-long.c line 43
Quoted 10 lines
tests/tcg/s390x/branch-relative-long.c new file +10
-0+ +int main (void ) +{ +size_t length =0x100000006 ; +unsigned char *buf; +void (*code)(void ); +size_t i; + + buf =mmap (NULL , length,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 );
With MAP_NORESERVE
Thanks!
Signature · 3 lines
Thanks, David / dhildenb