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.
Signed-off-by: Brian Cain <[email protected]> --- tests/tcg/hexagon/invalid-slots.c | 76 ++++++++++++++++++++++++------- tests/tcg/hexagon/Makefile.target | 6 --- 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/tests/tcg/hexagon/invalid-slots.c b/tests/tcg/hexagon/invalid-slots.c index 366ce4f42f..607027f314 100644 --- a/tests/tcg/hexagon/invalid-slots.c +++ b/tests/tcg/hexagon/invalid-slots.c @@ -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; } diff --git a/tests/tcg/hexagon/Makefile.target b/tests/tcg/hexagon/Makefile.target index 16669e04a8..d64aeba090 100644 --- a/tests/tcg/hexagon/Makefile.target +++ b/tests/tcg/hexagon/Makefile.target @@ -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 -- 2.34.1
