Handle some invalid encodings
v4 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.
v2: use a signal handler for SIGILL, combine the tests. S-b Pierrick.
v3:
Dropped "Reject duplex encodings" and added "Detect register write conflicts..." -> more general algo S-b Taylor. This patch moved over the test_zero() case covering the same 0x00000000 inst encoding R-b Alex.
v4:
Reg write conflict moved into decode.c, S-b Taylor.
Brian Cain (4):
target/hexagon: Fix invalid duplex decoding target/hexagon: Return decode failure for invalid non-duplex encodings tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test target/hexagon: Detect register write conflicts
linux-user/hexagon/cpu_loop.c | 5 + target/hexagon/cpu_bits.h | 1 + target/hexagon/decode.c | 70 ++++++++++- target/hexagon/gen_trans_funcs.py | 10 ++ target/hexagon/insn.h | 4 + target/hexagon/translate.c | 28 ++++- tests/tcg/hexagon/Makefile.target | 8 +- tests/tcg/hexagon/invalid-encoding.c | 106 +++++++++++++++++ tests/tcg/hexagon/invalid-slots.c | 76 +++++++++--- tests/tcg/hexagon/multiple-writes.c | 169 +++++++++++++++++++++++++++ 10 files changed, 447 insertions(+), 30 deletions(-) create mode 100644 tests/tcg/hexagon/invalid-encoding.c create mode 100644 tests/tcg/hexagon/multiple-writes.c
Signature · 1 line
2.34.1
PATCH v4 1/4 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).
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/Makefile.target | 1 + tests/tcg/hexagon/invalid-encoding.c | 81 ++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 tests/tcg/hexagon/invalid-encoding.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/Makefile.target +1 -0
@@ -51,6 +51,7 @@ HEX_TESTS += scatter_gather HEX_TESTS += hvx_misc HEX_TESTS += hvx_histogram HEX_TESTS += invalid-slots +HEX_TESTS += invalid-encoding HEX_TESTS += unaligned_pc run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \
tests/tcg/hexagon/invalid-encoding.c new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 2/4 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 | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
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 +25 -0
@@ -65,6 +65,30 @@ static int test_invalid_duplex(void)return sig; } +/* +* Invalid non-duplex encoding: +* 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. +*/ +static int test_invalid_nonduplex (void ) +{ +int sig; + + asmvolatile ( +"r0 = #0\n" +"r1 = ##1f\n" +"memw(%1) = r1\n" +".word 0xffffc000\n" +"1:\n" +"%0 = r0\n" + :"=r" (sig) + :"r" (&resume_pc) + :"r0" ,"r1" ,"memory" ); + +return sig; +} +int main () {struct sigaction act;
@@ -75,6 +99,7 @@ int main()assert (sigaction (SIGILL , &act,NULL ) ==0 );assert (test_invalid_duplex () ==SIGILL ); +assert (test_invalid_nonduplex () ==SIGILL );puts ("PASS" );return EXIT_SUCCESS ;
Signature · 1 line
2.34.1
PATCH v4 3/4 tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test
In reply to Brian CainRewrite invalid-slots.c to catch and verify SIGILL using a sigaction handler that modifies the ucontext, matching the pattern used by invalid-encoding.c.
--- tests/tcg/hexagon/Makefile.target | 6 --- tests/tcg/hexagon/invalid-slots.c | 76 ++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 23 deletions(-)
tests/tcg/hexagon/Makefile.target +0 -6
@@ -54,12 +54,6 @@ HEX_TESTS += invalid-slots HEX_TESTS += invalid-encoding 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) - -run-invalid-slots: invalid-slots - $(call run-and-check-exception, 0x15, $@, $(QEMU) $(QEMU_OPTS) $<) - HEX_TESTS += test_abs HEX_TESTS += test_bitcnt HEX_TESTS += test_bitsplit
tests/tcg/hexagon/invalid-slots.c +59 -17
88 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 4/4 target/hexagon: Detect register write conflicts
In reply to Brian CainA conflict exists when any GPR is written by multiple instructions and at least one write is unconditional. This catches (1) two unconditional writes to the same GPR and (2) an unconditional write combined with a predicated write.
Add HEX_CAUSE_REG_WRITE_CONFLICT and map it to SIGILL.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696
--- linux-user/hexagon/cpu_loop.c | 1 + target/hexagon/cpu_bits.h | 1 + target/hexagon/decode.c | 54 +++++++++ target/hexagon/gen_trans_funcs.py | 10 ++ target/hexagon/insn.h | 4 + target/hexagon/translate.c | 10 +- tests/tcg/hexagon/Makefile.target | 1 + tests/tcg/hexagon/multiple-writes.c | 169 ++++++++++++++++++++++++++++ 8 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/hexagon/multiple-writes.c
linux-user/hexagon/cpu_loop.c +1 -0
@@ -65,6 +65,7 @@ void cpu_loop(CPUHexagonState *env) env->gpr[HEX_REG_R31]);break ;case HEX_CAUSE_INVALID_PACKET : +case HEX_CAUSE_REG_WRITE_CONFLICT :force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , env->gpr[HEX_REG_PC ]);break ;
target/hexagon/cpu_bits.h +1 -0
@@ -34,6 +34,7 @@ enum hex_cause {HEX_CAUSE_FETCH_NO_UPAGE =0x012 ,HEX_CAUSE_INVALID_PACKET =0x015 ,HEX_CAUSE_INVALID_OPCODE =0x015 , +HEX_CAUSE_REG_WRITE_CONFLICT =0x01d ,HEX_CAUSE_PC_NOT_ALIGNED =0x01e ,HEX_CAUSE_PRIV_NO_UREAD =0x024 , HEX_CAUSE_PRIV_NO_UWRITE = 0x025,
target/hexagon/decode.c +54 -0
72 lines not on this page · raw message
target/hexagon/gen_trans_funcs.py +10 -0
@@ -91,6 +91,8 @@ def gen_trans_funcs(f): new_read_idx = -1 dest_idx = -1 dest_idx_reg_id = None + dest_is_pair = "false" + dest_is_gpr = "false" has_pred_dest = "false" for regno, (reg_type, reg_id, *_) in enumerate(regs): reg = hex_common.get_register(tag, reg_type, reg_id)
@@ -104,6 +106,12 @@ def gen_trans_funcs(f): if dest_idx_reg_id is None or reg_id < dest_idx_reg_id: dest_idx = regno dest_idx_reg_id = reg_id + dest_is_pair = ("true" + if isinstance(reg, hex_common.Pair) + else "false") + dest_is_gpr = ("true" + if reg_type == "R" + else "false") if reg_type == "P" and reg.is_written() and not reg.is_read(): has_pred_dest = "true"
@@ -129,6 +137,8 @@ def gen_trans_funcs(f): f.write(code_fmt(f"""\ insn->new_read_idx = {new_read_idx}; insn->dest_idx = {dest_idx}; + insn->dest_is_pair = {dest_is_pair}; + insn->dest_is_gpr = {dest_is_gpr}; insn->has_pred_dest = {has_pred_dest}; """)) f.write(textwrap.dedent(f"""\
target/hexagon/insn.h +4 -0
@@ -41,6 +41,8 @@ struct Instruction { uint32_t new_value_producer_slot:4;int32_t new_read_idx;int32_t dest_idx; +bool dest_is_pair; +bool dest_is_gpr;bool has_pred_dest;bool part1;/*
@@ -72,6 +74,8 @@ struct Packet {bool pkt_has_hvx;Insn *vhist_insn; +bool pkt_has_write_conflict; +Insn insn[INSTRUCTIONS_MAX ]; };
target/hexagon/translate.c +8 -2
@@ -943,7 +943,7 @@ static void gen_commit_packet(DisasContext *ctx)static void decode_and_translate_packet (CPUHexagonState *env,DisasContext *ctx) {uint32_t words[PACKET_WORDS_MAX ]; -int nwords; +int nwords, words_read;Packet pkt;int i;
@@ -954,8 +954,14 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx) } ctx->pkt = &pkt; -if (decode_packet (ctx, nwords, words, &pkt, false) >0 ) { + words_read =decode_packet (ctx, nwords, words, &pkt, false); +if (words_read >0 ) { pkt.pc = ctx->base.pc_next; +if (pkt.pkt_has_write_conflict) { +gen_exception_decode_fail (ctx, words_read, +HEX_CAUSE_REG_WRITE_CONFLICT ); +return ; + }gen_start_packet (ctx);for (i =0 ; i < pkt.num_insns; i++) { ctx->insn = &pkt.insn[i];
tests/tcg/hexagon/Makefile.target +1 -0
@@ -52,6 +52,7 @@ HEX_TESTS += hvx_misc HEX_TESTS += hvx_histogram HEX_TESTS += invalid-slots HEX_TESTS += invalid-encoding +HEX_TESTS += multiple-writes HEX_TESTS += unaligned_pc HEX_TESTS += test_abs
tests/tcg/hexagon/multiple-writes.c new file +169 -0
169 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v4 4/4] target/hexagon: Detect register write conflicts
Quoted 9 lines · Brian Cain
A conflict exists when any GPR is written by multiple instructions and at least one write is unconditional. This catches (1) two unconditional writes to the same GPR and (2) an unconditional write combined with a predicated write.
Add
HEX_CAUSE_REG_WRITE_CONFLICTand map it to SIGILL.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696
Signed-off-by Brian Cain <[email protected]>
Quoted 12 lines
--- linux-user/hexagon/cpu_loop.c | 1 + target/hexagon/cpu_bits.h | 1 + target/hexagon/decode.c | 54 +++++++++ target/hexagon/gen_trans_funcs.py | 10 ++ target/hexagon/insn.h | 4 + target/hexagon/translate.c | 10 +- tests/tcg/hexagon/Makefile.target | 1 + tests/tcg/hexagon/multiple-writes.c | 169 ++++++++++++++++++++++++++++ 8 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/hexagon/multiple-writes.c