Handle some invalid encodings
cover Handle some invalid encodings
Fix the issues reported by Alex Rønne Petersen causing zig test suite failures. The 0x0 case is a conflicting "multiple register write" - the handler here will cover duplexes but not the more general case of multiple register writes. I'll include that in a subsequent patchset.
Brian Cain (3):
target/hexagon: Fix invalid duplex decoding target/hexagon: Reject duplex encodings with duplicate dest registers target/hexagon: Return decode failure for invalid non-duplex encodings
linux-user/hexagon/cpu_loop.c | 4 ++++ target/hexagon/decode.c | 28 +++++++++++++++++++++++++--- target/hexagon/translate.c | 18 ++++++++++++++++-- tests/tcg/hexagon/invalid-duplex.c | 28 ++++++++++++++++++++++++++++ tests/tcg/hexagon/invalid-dups.c | 23 +++++++++++++++++++++++ tests/tcg/hexagon/invalid-encoding.c | 20 ++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 23 ++++++++++++++++++++++- 7 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 tests/tcg/hexagon/invalid-duplex.c create mode 100644 tests/tcg/hexagon/invalid-dups.c create mode 100644 tests/tcg/hexagon/invalid-encoding.c
Signature · 1 line
2.34.1
PATCH 1/3 target/hexagon: Fix invalid duplex decoding
When decoding a duplex instruction, if the slot0 sub-instruction fails to decode after slot1 succeeds, QEMU was leaving the packet in a partially-decoded state. This allowed invalid duplex encodings (where one sub-instruction doesn't match any valid pattern) to be executed incorrectly.
Fix by resetting the decoder state when slot0 fails, returning an empty instruction that triggers an exception.
Add gen_exception_decode_fail() for raising exceptions when decode fails before ctx->next_PC is initialized. This keeps gen_exception_end_tb() semantics unchanged (it continues to use ctx->next_PC for the exception PC after successful decode).
Update the invalid-slots test to expect SIGILL (exit code 132) instead of the old exit(1) behavior now that the exception is properly handled. Add invalid-duplex test for the specific case of invalid duplex encoding after immext.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3291
--- linux-user/hexagon/cpu_loop.c | 4 ++++ target/hexagon/decode.c | 13 +++++++++++-- target/hexagon/translate.c | 18 ++++++++++++++++-- tests/tcg/hexagon/invalid-duplex.c | 28 ++++++++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 11 ++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/tcg/hexagon/invalid-duplex.c
linux-user/hexagon/cpu_loop.c +4 -0
@@ -64,6 +64,10 @@ void cpu_loop(CPUHexagonState *env)force_sig_fault (TARGET_SIGBUS ,TARGET_BUS_ADRALN , env->gpr[HEX_REG_R31 ]);break ; +case HEX_CAUSE_INVALID_PACKET : +force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , + env->gpr[HEX_REG_PC ]); +break ;case EXCP_ATOMIC :cpu_exec_step_atomic (cs);break ;
target/hexagon/decode.c +11 -2
@@ -509,8 +509,14 @@ decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding) insn->iclass =iclass_bits (encoding);return 2 ; } +/* +* Slot0 decode failed after slot1 succeeded. This is an invalid +* duplex encoding (both sub-instructions must be valid). +*/ + ctx->insn = --insn; } -g_assert_not_reached (); +/* Invalid duplex encoding - return 0 to signal failure */ +return 0 ; } }
@@ -674,7 +680,10 @@ int decode_packet(DisasContext *ctx, int max_words, const uint32_t *words, encoding32 = words[words_read]; end_of_packet =is_packet_end (encoding32); new_insns =decode_insns (ctx, insn, encoding32); -g_assert (new_insns >0 ); +if (new_insns ==0 ) { +/* Invalid instruction encoding */ +return 0 ; + }/* * If we saw an extender, mark next word extended so immediate * decode works
target/hexagon/translate.c +16 -2
@@ -195,7 +195,21 @@ static void gen_exception_end_tb(DisasContext *ctx, int excp)tcg_gen_movi_tl (hex_gpr[HEX_REG_PC ], ctx->next_PC);gen_exception_raw (excp); ctx->base.is_jmp =DISAS_NORETURN ; +} +/* +* Generate exception for decode failures. Unlike gen_exception_end_tb, +* this is used when decode fails before ctx->next_PC is initialized. +*/ +static void gen_exception_decode_fail (DisasContext *ctx,int nwords,int excp) +{ +target_ulong fail_pc = ctx->base.pc_next + nwords *sizeof (uint32_t ); + +gen_exec_counters (ctx); +tcg_gen_movi_tl (hex_gpr[HEX_REG_PC ], fail_pc); +gen_exception_raw (excp); + ctx->base.is_jmp =DISAS_NORETURN ; + ctx->base.pc_next = fail_pc; } static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
@@ -935,7 +949,7 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx) nwords =read_packet_words (env, ctx, words);if (!nwords) { -gen_exception_end_tb (ctx,HEX_CAUSE_INVALID_PACKET ); +gen_exception_decode_fail (ctx,0 ,HEX_CAUSE_INVALID_PACKET );return ; }
@@ -950,7 +964,7 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)gen_commit_packet (ctx); ctx->base.pc_next += pkt.encod_pkt_size_in_bytes; }else { -gen_exception_end_tb (ctx,HEX_CAUSE_INVALID_PACKET ); +gen_exception_decode_fail (ctx, nwords,HEX_CAUSE_INVALID_PACKET ); } }
tests/tcg/hexagon/invalid-duplex.c new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Test that invalid duplex encodings are properly rejected. +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* +* This test uses an invalid packet encoding where: +* - Word 0: 0x0fff6fff = immext(#0xfffbffc0), parse bits = 01 +* - Word 1: 0x600237b0 = duplex with: +* - slot0 = 0x17b0 (invalid S2 subinstruction encoding) +* - slot1 = 0x0002 (valid SA1_addi) +* - duplex iclass = 7 (S2 for slot0, A for slot1) +* +* Since slot0 doesn't decode to any valid S2 subinstruction, this packet +* should be rejected and raise SIGILL. +*/ + +int main () +{ + asmvolatile ( +/* Invalid packet: immext followed by duplex with invalid slot0 */ +".word 0x0fff6fff\n" /* immext(#0xfffbffc0), parse=01 */ +".word 0x600237b0\n" /* duplex: slot0=0x17b0 (invalid), slot1=0x0002 */ + : : :"memory" ); +return 0 ; +}
tests/tcg/hexagon/Makefile.target +10 -1
@@ -51,13 +51,22 @@ HEX_TESTS += scatter_gather HEX_TESTS += hvx_misc HEX_TESTS += hvx_histogram HEX_TESTS += invalid-slots +HEX_TESTS += invalid-duplex HEX_TESTS += unaligned_pc run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \ test $$? -eq 1 && grep -q "exception $(strip $1)" $2.stderr) +# Check that command dies from SIGILL (exit code = 128 + 4 = 132) run-invalid-slots: invalid-slots - $(call run-and-check-exception, 0x15, $@, $(QEMU) $(QEMU_OPTS) $<) + $(call quiet-command, \ + $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ + TEST, invalid-slots on $(TARGET_NAME)) + +run-invalid-duplex: invalid-duplex + $(call quiet-command, \ + $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ + TEST, invalid-duplex on $(TARGET_NAME)) HEX_TESTS += test_abs HEX_TESTS += test_bitcnt
Signature · 1 line
2.34.1
Re: [PATCH 1/3] target/hexagon: Fix invalid duplex decoding
Quoted 21 lines · Brian Cain
When decoding a duplex instruction, if the slot0 sub-instruction fails to decode after slot1 succeeds, QEMU was leaving the packet in a partially-decoded state. This allowed invalid duplex encodings (where one sub-instruction doesn't match any valid pattern) to be executed incorrectly.
Fix by resetting the decoder state when slot0 fails, returning an empty instruction that triggers an exception.
Add
gen_exception_decode_fail()for raising exceptions when decode fails beforectx->next_PCis initialized. This keepsgen_exception_end_tb()semantics unchanged (it continues to usectx->next_PCfor the exception PC after successful decode).Update the invalid-slots test to expect SIGILL (exit code 132) instead of the old exit(1) behavior now that the exception is properly handled. Add invalid-duplex test for the specific case of invalid duplex encoding after immext.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3291
Signed-off-by Brian Cain <[email protected]>
Quoted 9 lines
--- linux-user/hexagon/cpu_loop.c | 4 ++++ target/hexagon/decode.c | 13 +++++++++++-- target/hexagon/translate.c | 18 ++++++++++++++++-- tests/tcg/hexagon/invalid-duplex.c | 28 ++++++++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 11 ++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-)create mode 100644
tests/tcg/hexagon/invalid-duplex.c
PATCH 2/3 target/hexagon: Reject duplex encodings with duplicate dest registers
In reply to Brian CainA duplex encoding like 0x00000000 decodes as two loads that both write r0.
Add a check in decode_insns() after both sub-instructions decode successfully to verify they don't write the same destination register.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696
--- target/hexagon/decode.c | 12 ++++++++++++ tests/tcg/hexagon/invalid-dups.c | 23 +++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 6 ++++++ 3 files changed, 41 insertions(+) create mode 100644 tests/tcg/hexagon/invalid-dups.c
target/hexagon/decode.c +12 -0
@@ -501,12 +501,24 @@ decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)/* The slot1 subinsn needs to be in the packet first */ if (decode_slot1_subinsn (ctx, slot1_subinsn)) { +Insn *slot1_insn = insn; insn->generate = opcode_genptr[insn->opcode]; insn->iclass =iclass_bits (encoding); ctx->insn = ++insn;if (decode_slot0_subinsn (ctx, slot0_subinsn)) { insn->generate = opcode_genptr[insn->opcode]; insn->iclass =iclass_bits (encoding); +/* +* Check that the two sub-instructions don't write the same +* destination register (e.g., encoding 0x0 decodes as two +* loads both writing R0, which is an invalid packet). +*/ +if (insn->dest_idx >=0 && slot1_insn->dest_idx >=0 && + insn->regno[insn->dest_idx] == + slot1_insn->regno[slot1_insn->dest_idx]) { + ctx->insn = --insn; +return 0 ; + }return 2 ; }/*
tests/tcg/hexagon/invalid-dups.c new file +23 -0
@@ -0,0 +1,23 @@ +/* +* Test that duplex encodings with duplicate destination registers are rejected. +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* +* The encoding 0x00000000 decodes as a duplex with parse bits [15:14] = 0b00: +* slot1: SL1_loadri_io R0 = memw(R0+#0x0) +* slot0: SL1_loadri_io R0 = memw(R0+#0x0) +* +* Both sub-instructions write R0, which is an invalid packet (duplicate +* destination register). This should raise SIGILL. +*/ + +int main () +{ + asmvolatile ( +".word 0x00000000\n" + : : :"r0" ,"memory" ); +return 0 ; +}
tests/tcg/hexagon/Makefile.target +6 -0
@@ -52,6 +52,7 @@ HEX_TESTS += hvx_misc HEX_TESTS += hvx_histogram HEX_TESTS += invalid-slots HEX_TESTS += invalid-duplex +HEX_TESTS += invalid-dups HEX_TESTS += unaligned_pc run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \
@@ -68,6 +69,11 @@ run-invalid-duplex: invalid-duplex $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ TEST, invalid-duplex on $(TARGET_NAME)) +run-invalid-dups: invalid-dups + $(call quiet-command, \ + $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ + TEST, invalid-dups on $(TARGET_NAME)) + HEX_TESTS += test_abs HEX_TESTS += test_bitcnt HEX_TESTS += test_bitsplit
Signature · 1 line
2.34.1
Re: [PATCH 2/3] target/hexagon: Reject duplex encodings with duplicate dest registers
Quoted 5 lines · Brian Cain
A duplex encoding like
0x00000000decodes as two loads that both write r0.Add a check in
decode_insns()after both sub-instructions decode successfully to verify they don't write the same destination register.
Quoted 9 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696
Signed-off-by Brian Cain <[email protected]>--- target/hexagon/decode.c | 12 ++++++++++++ tests/tcg/hexagon/invalid-dups.c | 23 +++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 6 ++++++ 3 files changed, 41 insertions(+)create mode 100644
tests/tcg/hexagon/invalid-dups.c
PATCH 3/3 target/hexagon: Return decode failure for invalid non-duplex encodings
In reply to Brian CainWhen a non-duplex encoding (parse_bits != 0) fails both decode_normal() and decode_hvx(), the decoder hit an unreachable. Instead, handle the decode failure and raise an exception.
--- target/hexagon/decode.c | 3 ++- tests/tcg/hexagon/invalid-encoding.c | 20 ++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/hexagon/invalid-encoding.c
target/hexagon/decode.c +2 -1
@@ -489,7 +489,8 @@ decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding) insn->iclass =iclass_bits (encoding);return 1 ; } -g_assert_not_reached (); +/* Invalid non-duplex encoding */ +return 0 ; }else {uint32_t iclass =get_duplex_iclass (encoding);unsigned int slot0_subinsn =get_slot0_subinsn (encoding);
tests/tcg/hexagon/invalid-encoding.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* Test that invalid non-duplex encodings are properly rejected. +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* +* The encoding 0xffffc000 has parse bits [15:14] = 0b11, making it a +* non-duplex instruction and packet end. The remaining bits do not match +* any valid normal or HVX instruction encoding, so this should raise SIGILL. +*/ + +int main () +{ + asmvolatile ( +".word 0xffffc000\n" + : : :"memory" ); +return 0 ; +}
tests/tcg/hexagon/Makefile.target +6 -0
@@ -53,6 +53,7 @@ HEX_TESTS += hvx_histogram HEX_TESTS += invalid-slots HEX_TESTS += invalid-duplex HEX_TESTS += invalid-dups +HEX_TESTS += invalid-encoding HEX_TESTS += unaligned_pc run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \
@@ -74,6 +75,11 @@ run-invalid-dups: invalid-dups $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ TEST, invalid-dups on $(TARGET_NAME)) +run-invalid-encoding: invalid-encoding + $(call quiet-command, \ + $(QEMU) $(QEMU_OPTS) $< ; test $$? -eq 132, \ + TEST, invalid-encoding on $(TARGET_NAME)) + HEX_TESTS += test_abs HEX_TESTS += test_bitcnt HEX_TESTS += test_bitsplit
Signature · 1 line
2.34.1
Re: [PATCH 3/3] target/hexagon: Return decode failure for invalid non-duplex encodings
Quoted 4 lines · Brian Cain
When a non-duplex encoding (
parse_bits!= 0) fails bothdecode_normal()anddecode_hvx(), the decoder hit an unreachable. Instead, handle the decode failure and raise an exception.
Quoted 8 lines
Signed-off-by Brian Cain <[email protected]>--- target/hexagon/decode.c | 3 ++- tests/tcg/hexagon/invalid-encoding.c | 20 ++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-)create mode 100644
tests/tcg/hexagon/invalid-encoding.c
Quoted 18 lines · Brian Cain
Fix the issues reported by Alex Rønne Petersen causing zig test suite failures. The
0x0case is a conflicting "multiple register write" - the handler here will cover duplexes but not the more general case of multiple register writes. I'll include that in a subsequent patchset.Brian Cain (3):
target/hexagon: Fix invalid duplex decoding target/hexagon: Reject duplex encodings with duplicate dest registers target/hexagon: Return decode failure for invalid non-duplex encodingslinux-user/hexagon/cpu_loop.c | 4 ++++ target/hexagon/decode.c | 28 +++++++++++++++++++++++++--- target/hexagon/translate.c | 18 ++++++++++++++++-- tests/tcg/hexagon/invalid-duplex.c | 28 ++++++++++++++++++++++++++++ tests/tcg/hexagon/invalid-dups.c | 23 +++++++++++++++++++++++ tests/tcg/hexagon/invalid-encoding.c | 20 ++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 23 ++++++++++++++++++++++- 7 files changed, 138 insertions(+), 6 deletions(-)
Quoted 4 lines
create mode 100644
tests/tcg/hexagon/invalid-duplex.ccreate mode 100644tests/tcg/hexagon/invalid-dups.ccreate mode 100644tests/tcg/hexagon/invalid-encoding.c
Sounds good.
I have one suggestion to improve it. For tests, it would be nice to catch SIGILL, so test returns 0 if it worked as expected, or exit with 1 if SIGILL was not sent. It's just a suggestion though :)
Regards,
Pierrick
Quoted 25 lines · Pierrick Bouvier
Quoted 18 lines · Brian Cain
Fix the issues reported by Alex Rønne Petersen causing zig test suite failures. The
0x0case is a conflicting "multiple register write" - the handler here will cover duplexes but not the more general case of multiple register writes. I'll include that in a subsequent patchset.Brian Cain (3):
target/hexagon: Fix invalid duplex decoding target/hexagon: Reject duplex encodings with duplicate dest registers target/hexagon: Return decode failure for invalid non-duplex encodingslinux-user/hexagon/cpu_loop.c | 4 ++++ target/hexagon/decode.c | 28 +++++++++++++++++++++++++--- target/hexagon/translate.c | 18 ++++++++++++++++-- tests/tcg/hexagon/invalid-duplex.c | 28 ++++++++++++++++++++++++++++ tests/tcg/hexagon/invalid-dups.c | 23 +++++++++++++++++++++++ tests/tcg/hexagon/invalid-encoding.c | 20 ++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 23 ++++++++++++++++++++++- 7 files changed, 138 insertions(+), 6 deletions(-)Quoted 4 lines
create mode 100644
tests/tcg/hexagon/invalid-duplex.ccreate mode 100644tests/tcg/hexagon/invalid-dups.ccreate mode 100644tests/tcg/hexagon/invalid-encoding.c
Quoted 5 lines
Sounds good.
I have one suggestion to improve it. For tests, it would be nice to catch SIGILL, so test returns 0 if it worked as expected, or exit with 1 if SIGILL was not sent. It's just a suggestion though :)
Tsk - good call. And that way I could combine the test cases. I like it, will do.
Quoted 2 lines
Regards,
Pierrick