From 86851434c8372fdd1f4f078d92a6e72a92d5ac0d Mon Sep 17 00:00:00 2001 From: Enjia Mai Date: Thu, 25 Mar 2021 01:01:01 +0800 Subject: [PATCH] tests: ztest: do not optimize the divide by zero test case Make the divide instruction not being optimized of the ztest example test case. Do this because it will be replaced by invalid opcode exception instruction generated by GCC. Fixes #33403. Signed-off-by: Enjia Mai --- tests/ztest/error_hook/src/main.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/ztest/error_hook/src/main.c b/tests/ztest/error_hook/src/main.c index 86fa0c8102b..a8244298b77 100644 --- a/tests/ztest/error_hook/src/main.c +++ b/tests/ztest/error_hook/src/main.c @@ -41,7 +41,11 @@ static void trigger_assert_fail(void *a) __ASSERT(a != NULL, "parameter a should not be NULL!"); } -static void trigger_fault_illegal_instruction(void) +/* + * Do not optimize to prevent GCC from generating invalid + * opcode exception instruction instead of real instruction. + */ +__no_optimization static void trigger_fault_illegal_instruction(void) { void *a = NULL; @@ -49,7 +53,11 @@ static void trigger_fault_illegal_instruction(void) ((void(*)(void))&a)(); } -static void trigger_fault_access(void) +/* + * Do not optimize to prevent GCC from generating invalid + * opcode exception instruction instead of real instruction. + */ +__no_optimization static void trigger_fault_access(void) { #if defined(CONFIG_SOC_ARC_IOT) || defined(CONFIG_SOC_NSIM) || defined(CONFIG_SOC_EMSK) /* For iotdk, em_starterkit and ARC/nSIM, nSIM simulates full address space of @@ -82,7 +90,12 @@ static void trigger_fault_access(void) printk("b is %d\n", b); } -static void trigger_fault_divide_zero(void) + +/* + * Do not optimize the divide instruction. GCC will generate invalid + * opcode exception instruction instead of real divide instruction. + */ +__no_optimization static void trigger_fault_divide_zero(void) { int a = 1; int b = 0; @@ -90,6 +103,18 @@ static void trigger_fault_divide_zero(void) /* divide by zero */ a = a / b; printk("a is %d\n", a); + +/* + * While no optimization is enabled, some QEMU such as QEMU cortex a53 + * series and QEMU mps2 series board will not trigger an exception for + * divide zero. They might need to enable the divide zero exception. + * We only skip the QEMU board here, this means this test will still + * apply on the physical board. + */ +#if (defined(CONFIG_SOC_SERIES_MPS2) && (CONFIG_QEMU_TARGET)) || \ + (CONFIG_BOARD_QEMU_CORTEX_A53) + ztest_test_skip(); +#endif } static void trigger_fault_oops(void)