Fix BRASL and BRCL with large negative offsets
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.
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 | 29 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/s390x/branch-relative-long.c
Signature · 1 line
2.35.1
PATCH 3/3 tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
Add a small test in order to prevent regressions.
--- tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/branch-relative-long.c | 29 ++++++++++++++++++++++++++ 2 files changed, 30 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 +29 -0
@@ -0,0 +1,29 @@ +#include <assert.h> +#include <stddef.h> +#include <sys/mman.h> + +int main (void ) +{ +const unsigned short opcodes[] = { +0xc005 ,/* brasl %r0 */ +0xc0f4 ,/* brcl 0xf */ + }; +size_t length =0x100000006 ; +unsigned char *buf; +int i; + + buf =mmap (NULL , length,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +assert (buf !=MAP_FAILED ); + + *(unsigned short *)&buf[0 ] =0x07fe ;/* br %r14 */ + *(unsigned int *)&buf[0x100000002 ] =0x80000000 ; +for (i =0 ; i <sizeof (opcodes) /sizeof (opcodes[0 ]); i++) { + *(unsigned short *)&buf[0x100000000 ] = opcodes[i]; + ((void (*)(void ))&buf[0x100000000 ])(); + } + +munmap (buf, length); + +return 0 ; +}
Signature · 1 line
2.35.1
PATCH 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 1/3 s390x/tcg: Fix BRASL 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
@@ -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
Re: [PATCH 1/3] s390x/tcg: Fix BRASL with a large negative offset
Quoted 14 lines · Ilya Leoshkevich
When RI2 is
0x80000000, qemu enters an infinite loop instead of jumping backwards. Fix by adding a missing cast, like inin2_ri2().Fixes 8ac33cdb8bfb ("Convert BRANCH AND SAVE")Signed-off-by Ilya Leoshkevich <[email protected]>--- target/s390x/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/s390x/tcg/translate.c +0
-0
target/s390x/tcg/translate.c static DisasJumpType op_bal(DisasContext *s, DisasOps *o) · line 1603
Quoted 8 lines
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)
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 2/3] s390x/tcg: Fix BRCL with a large negative offset
In reply to Ilya LeoshkevichQuoted 14 lines · Ilya Leoshkevich
When RI2 is
0x80000000, qemu enters an infinite loop instead of jumping backwards. Fix by adding a missing cast, like inin2_ri2().Fixes 7233f2ed1717 ("target-s390: Convert BRANCH ON CONDITION")Signed-off-by Ilya Leoshkevich <[email protected]>--- target/s390x/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/s390x/tcg/translate.c +0
-0
target/s390x/tcg/translate.c static DisasJumpType help_branch(DisasContext *s, DisasCompare *c, · line 1207
Quoted 8 lines
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 · 3 lines
Thanks, David / dhildenb
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
In reply to Ilya LeoshkevichQuoted 41 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 | 29 ++++++++++++++++++++++++++ 2 files changed, 30 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 +14
-0@@ -0,0 +1,29 @@ +#include <assert.h> +#include <stddef.h> +#include <sys/mman.h> + +int main (void ) +{ +const unsigned short opcodes[] = { +0xc005 ,/* brasl %r0 */ +0xc0f4 ,/* brcl 0xf */ + }; +size_t length =0x100000006 ; +unsigned char *buf; +int i; +
tests/tcg/s390x/branch-relative-long.c line 24
Quoted 10 lines
tests/tcg/s390x/branch-relative-long.c new file +10
-0+ buf =mmap (NULL , length,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +assert (buf !=MAP_FAILED ); + + *(unsigned short *)&buf[0 ] =0x07fe ;/* br %r14 */ + *(unsigned int *)&buf[0x100000002 ] =0x80000000 ; +for (i =0 ; i <sizeof (opcodes) /sizeof (opcodes[0 ]); i++) { + *(unsigned short *)&buf[0x100000000 ] = opcodes[i]; + ((void (*)(void ))&buf[0x100000000 ])(); + }
Hmmm, can't we write some "nice" inline asm instead?
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
Quoted 49 lines · David Hildenbrand
Quoted 48 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 | 29
++++++++++++++++++++++++++2 files changed, 30 insertions(+)create mode 100644
tests/tcg/s390x/branch-relative-long.ctests/tcg/s390x/Makefile.target +0
-0@@ -15,6 +15,7 @@ TESTS+=mvcTESTS+=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 +18
-0@@ -0,0 +1,29 @@ +#include <assert.h> +#include <stddef.h> +#include <sys/mman.h> + +int main (void ) +{ + const unsigned short opcodes[] = { + 0xc005, /* brasl %r0 */ + 0xc0f4, /* brcl 0xf */ + }; + size_t length = 0x100000006; + unsigned char *buf; + int i; + + buf = mmap(NULL, length, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + assert(buf != MAP_FAILED); +
Quoted 10 lines
tests/tcg/s390x/branch-relative-long.c line 24
Quoted 6 lines
tests/tcg/s390x/branch-relative-long.c new file +6
-0+ *(unsigned short *)&buf[0] = 0x07fe; /* br %r14 */ + *(unsigned int *)&buf[0x100000002] = 0x80000000; + for (i = 0; i < sizeof(opcodes) / sizeof(opcodes[0]); i++) { + *(unsigned short *)&buf[0x100000000] = opcodes[i]; + ((void (*)(void))&buf[0x100000000])(); + }Hmmm, can't we write some "nice" inline asm instead?
If we do this in a straightforward way, then the resulting binary will be 4G large.
But maybe there is a way to play games with sections, I'll need to think about it.
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
In reply to Ilya Leoshkevichtests/tcg/s390x/branch-relative-long.c line 17
Quoted 7 lines · Ilya Leoshkevich
tests/tcg/s390x/branch-relative-long.c new file +7
-0+size_t length =0x100000006 ; +unsigned char *buf; +int i; + + buf =mmap (NULL , length,PROT_READ |PROT_WRITE |PROT_EXEC , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +assert (buf !=MAP_FAILED );
I'm thinking exit success here, as such a large allocation may well fail depending on the host.
r~
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
Am 11.03.22 um 21:32 schrieb Richard Henderson:
Quoted 10 lines · Ilya Leoshkevich
Quoted 7 lines
+
size_tlength =0x100000006;
+ unsigned char *buf;
+ int i;
+
+ buf = mmap(NULL, length,PROT_READ|PROT_WRITE|PROT_EXEC, +MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+assert(buf != MAP_FAILED);I'm thinking exit success here, as such a large allocation may well fail depending on the host.
What about using MAP_NORESERVE ?
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
Quoted 12 lines · Christian Borntraeger
Am 11.03.22 um 21:32 schrieb Richard Henderson:
Quoted 9 lines · Ilya Leoshkevich
Quoted 7 lines
+
size_tlength =0x100000006;
+ unsigned char *buf;
+ int i;
+
+ buf = mmap(NULL, length,PROT_READ|PROT_WRITE|PROT_EXEC, +MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+assert(buf != MAP_FAILED);
Quoted 3 lines
Quoted 1 line
I'm thinking exit success here, as such a large allocation may well fail depending on the host.
What about using
MAP_NORESERVE?
+1
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 3/3] tests/tcg/s390x: Test BRASL and BRCL with large negative offsets
In reply to Christian BorntraegerQuoted 12 lines · Christian Borntraeger
Am 11.03.22 um 21:32 schrieb Richard Henderson:
Quoted 9 lines · Ilya Leoshkevich
Quoted 7 lines
+
size_tlength =0x100000006;
+ unsigned char *buf;
+ int i;
+
+ buf = mmap(NULL, length,PROT_READ|PROT_WRITE|PROT_EXEC, +MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+assert(buf != MAP_FAILED);
Quoted 3 lines
Quoted 1 line
I'm thinking exit success here, as such a large allocation may well fail depending on the host.
What about using
MAP_NORESERVE?
That can help, certainly. But that doesn't affect RLIMIT_AS, or a 32-bit host.
r~