Skip to contentSkip to conversation

Handle some invalid encodings

PATCH Superseded Brian Cain 3 participants 12 messages Feb 7

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 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.

Brian Cain (4):
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 tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

 linux-user/hexagon/cpu_loop.c        |   4 +
 target/hexagon/decode.c              |  28 +++++-
 target/hexagon/translate.c           |  18 +++-
 tests/tcg/hexagon/invalid-encoding.c | 135 +++++++++++++++++++++++++++
 tests/tcg/hexagon/invalid-slots.c    |  76 +++++++++++----
 tests/tcg/hexagon/Makefile.target    |   7 +-
 6 files changed, 240 insertions(+), 28 deletions(-)
 create mode 100644 tests/tcg/hexagon/invalid-encoding.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 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/invalid-encoding.c | 81 ++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target    |  1 +
 5 files changed, 113 insertions(+), 4 deletions(-)
 create mode 100644 tests/tcg/hexagon/invalid-encoding.c
linux-user/hexagon/cpu_loop.c +4 -0
index 1941f4c9c1..c0e1098e3f 100644
@@ -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
index b5ece60450..69ba1ec96c 100644
@@ -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
index e88e19cc1a..7fe8b35351 100644
@@ -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-encoding.c new file +81 -0

81 lines not on this page · raw message

tests/tcg/hexagon/Makefile.target +1 -0
index e5182c01d8..16669e04a8 100644
@@ -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; \
Signature · 1 line
2.34.1
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] target/hexagon: Fix invalid duplex decoding

Quoted 40 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 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/invalid-encoding.c | 81 ++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target    |  1 +
 5 files changed, 113 insertions(+), 4 deletions(-)
 create mode 100644 tests/tcg/hexagon/invalid-encoding.c
target/hexagon/decode.c +2 -0
index b5ece60450..69ba1ec96c 100644
@@ -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

target/hexagon/decode.c decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding) · line 516

Quoted 3 lines
target/hexagon/decode.c +3 -0
+             * duplex encoding (both sub-instructions must be valid).
+             */
+            ctx->insn = --insn;

Why is this needed?

Otherwise

author
#3 lore raw

PATCH v2 2/4 target/hexagon: Reject duplex encodings with duplicate dest registers

In reply to Brian Cain

A 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-encoding.c | 29 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
target/hexagon/decode.c +12 -0
index 69ba1ec96c..90499fc320 100644
@@ -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-encoding.c +29 -0
index 010a5eb741..1bbd312b61 100644
@@ -65,6 +65,34 @@ static int test_invalid_duplex(void)
     return sig;
 }
 
+/*
+ * Duplex with duplicate destination registers (issue #2696):
+ * 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.
+ */
+static int test_invalid_dups(void)
+{
+    int sig;
+
+    asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%1) = r1\n"
+        ".word 0x00000000\n"
+        "1:\n"
+        "%0 = r0\n"
+        : "=r"(sig)
+        : "r"(&resume_pc)
+        : "r0", "r1", "memory");
+
+    return sig;
+}
+
 int main()
 {
     struct sigaction act;
@@ -75,6 +103,7 @@ int main()
     assert(sigaction(SIGILL, &act, NULL) == 0);
 
     assert(test_invalid_duplex() == SIGILL);
+    assert(test_invalid_dups() == SIGILL);
 
     puts("PASS");
     return EXIT_SUCCESS;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/4] target/hexagon: Reject duplex encodings with duplicate dest registers

Quoted 42 lines · Brian Cain

A 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-encoding.c | 29 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
target/hexagon/decode.c +3 -0
index 69ba1ec96c..90499fc320 100644
@@ -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

target/hexagon/decode.c +9 -0
+                 * 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;
+                }
Quoted 1 line

Isn't this a more general problem than what is checked here? What if two

non-duplex instructions write the same register? What if an instruction writes more than one register (e.g., post-increment load)?

Let the decoding go ahead and finish, then add a check for duplicate writes for the whole packet. Look at ctx_log_reg_write - called during analyze_packet.

Thanks,
Taylor

author
#5 lore raw

Re: [PATCH v2 2/4] target/hexagon: Reject duplex encodings with duplicate dest registers

Quoted 38 lines · Taylor Simpson
Quoted 34 lines · Brian Cain

A 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-encoding.c | 29 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
target/hexagon/decode.c +6 -0
index 69ba1ec96c..90499fc320 100644
@@ -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).
+                 */
Quoted 8 lines

target/hexagon/decode.c decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)

Quoted 7 lines
target/hexagon/decode.c +6 -0
+                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;
+                }
 

Isn't this a more general problem than what is checked here? What if two non-duplex instructions write the same register? What if an instruction writes more than one register (e.g., post-increment load)?

It is, yeah. I mentioned in the cover letter that I planned to land that later.

But ok -- I can move it here into this series.

Quoted 5 lines

Let the decoding go ahead and finish, then add a check for duplicate writes for the whole packet. Look at ctx_log_reg_write - called during analyze_packet.

Thanks,
Taylor

#6 lore raw

Re: [PATCH v2 2/4] target/hexagon: Reject duplex encodings with duplicate dest registers

Quoted 52 lines · Brian Cain
Quoted 4 lines · Taylor Simpson

On Sat, Feb 7, 2026 at 11:06 AM Brian Cain <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

A duplex encoding like 0x00000000 decodes as two loads that both write

r0.

Quoted 12 lines
Quoted 12 lines

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-encoding.c | 29 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
Quoted 5 lines

target/hexagon/decode.c decode_insns(DisasContext *ctx, Insn *insn,

Quoted 5 lines
target/hexagon/decode.c +0 -0
index 69ba1ec96c..90499fc320 100644
@@ -501,12 +501,24 @@ decode_insns(DisasContext *ctx, Insn *insn,

uint32_t encoding)

Quoted 12 lines

target/hexagon/decode.c decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)

Quoted 12 lines
target/hexagon/decode.c +3 -0
         /* 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

Quoted 1 line

target/hexagon/decode.c decode_insns(DisasContext *ctx, Insn *insn, · line 512

Quoted 1 line
target/hexagon/decode.c +1 -0
+                 * destination register (e.g., encoding 0x0 decodes as

two

Quoted 9 lines
Quoted 9 lines
target/hexagon/decode.c +8 -0
+                 * 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;
+                }
 
Quoted 9 lines
Quoted 1 line

Isn't this a more general problem than what is checked here? What if

two non-duplex instructions write the same register? What if an instruction writes more than one register (e.g., post-increment load)?

It is, yeah. I mentioned in the cover letter that I planned to land that later.

But ok -- I can move it here into this series.

Sorry, I missed that comment in the cover letter.

Taylor

author
#7 lore raw

PATCH v2 3/4 target/hexagon: Return decode failure for invalid non-duplex encodings

In reply to Brian Cain

When 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
index 90499fc320..ebb4e02a17 100644
@@ -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
index 1bbd312b61..040a7d9147 100644
@@ -93,6 +93,30 @@ static int test_invalid_dups(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;
+
+    asm volatile(
+        "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;
@@ -104,6 +128,7 @@ int main()
 
     assert(test_invalid_duplex() == SIGILL);
     assert(test_invalid_dups() == SIGILL);
+    assert(test_invalid_nonduplex() == SIGILL);
 
     puts("PASS");
     return EXIT_SUCCESS;
Signature · 1 line
2.34.1
Reviewed-by on v2 3/4
#8 lore raw

Re: [PATCH v2 3/4] target/hexagon: Return decode failure for invalid non-duplex encodings

Quoted 11 lines · Brian Cain

When 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(-)
author
#9 lore raw

PATCH v2 4/4 tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

In reply to Brian Cain

Rewrite 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/invalid-slots.c | 76 ++++++++++++++++++++++++-------
 tests/tcg/hexagon/Makefile.target |  6 ---
 2 files changed, 59 insertions(+), 23 deletions(-)
tests/tcg/hexagon/invalid-slots.c +59 -17
index 366ce4f42f..607027f314 100644
@@ -1,29 +1,71 @@
 /*
- *  Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+ * Test that invalid slot assignments are properly rejected.
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#include <assert.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void *resume_pc;
+
+static void handle_sigill(int sig, siginfo_t *info, void *puc)
+{
+    ucontext_t *uc = (ucontext_t *)puc;
+
+    if (sig != SIGILL) {
+        _exit(EXIT_FAILURE);
+    }
+
+    uc->uc_mcontext.r0 = SIGILL;
+    uc->uc_mcontext.pc = (unsigned long)resume_pc;
+}
+
 char mem[8] __attribute__((aligned(8)));
 
-int main()
+/*
+ * Invalid packet with 2 instructions at slot 0:
+ * - Word 0: 0xa1804100 = memw(r0) = r1
+ * - Word 1: 0x28032804 = { r3 = #0; r4 = #0 }
+ *
+ * This should raise SIGILL due to the invalid slot assignment.
+ */
+static int test_invalid_slots(void)
 {
+    int sig;
+
     asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%1) = r1\n"
         "r0 = #mem\n"
-        /* Invalid packet (2 instructions at slot 0): */
         ".word 0xa1804100\n" /* { memw(r0) = r1;      */
         ".word 0x28032804\n" /*   r3 = #0; r4 = #0 }  */
-        : : : "r0", "r3", "r4", "memory");
-    return 0;
+        "1:\n"
+        "%0 = r0\n"
+        : "=r"(sig)
+        : "r"(&resume_pc)
+        : "r0", "r1", "r3", "r4", "memory");
+
+    return sig;
+}
+
+int main()
+{
+    struct sigaction act;
+
+    memset(&act, 0, sizeof(act));
+    act.sa_sigaction = handle_sigill;
+    act.sa_flags = SA_SIGINFO;
+    assert(sigaction(SIGILL, &act, NULL) == 0);
+
+    assert(test_invalid_slots() == SIGILL);
+
+    puts("PASS");
+    return EXIT_SUCCESS;
 }
tests/tcg/hexagon/Makefile.target +0 -6
index 16669e04a8..d64aeba090 100644
@@ -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
Signature · 1 line
2.34.1
Reviewed-by on v2 4/4
#10 lore raw

Re: [PATCH v2 4/4] tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

Quoted 10 lines · Brian Cain

Rewrite 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/invalid-slots.c | 76 ++++++++++++++++++++++++-------
  tests/tcg/hexagon/Makefile.target |  6 ---
  2 files changed, 59 insertions(+), 23 deletions(-)
Reviewed-by on v2 4/4
#11 lore raw

Re: [PATCH v2 4/4] tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

In reply to Brian Cain
Quoted 10 lines · Brian Cain

Rewrite 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/invalid-slots.c | 76 ++++++++++++++++++++++++-------
 tests/tcg/hexagon/Makefile.target |  6 ---
 2 files changed, 59 insertions(+), 23 deletions(-)