From f2422f1f19cff69eda0909554b0846cb3712e91f Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Tue, 6 Aug 2019 12:29:48 -0700 Subject: [PATCH] tests: clean up fatal error handlers - k_sys_fatal_error_handler() can return on all platforms, indicating that the faulting thread should be aborted. - Hang the system for unexpected faults instead of trying to keep going, we have no idea whether the system is even runnable. Prevents infinite crash loops during tests. Signed-off-by: Andrew Boie --- tests/arch/x86/static_idt/src/main.c | 10 +++++-- .../mem_protect/mem_protect/src/common.c | 6 +--- .../kernel/mem_protect/protection/src/main.c | 11 ------- tests/kernel/mem_protect/stackprot/src/main.c | 5 +++- tests/kernel/mem_protect/userspace/src/main.c | 30 +++++++------------ tests/kernel/pipe/pipe/src/test_pipe.c | 6 +--- .../kernel/threads/dynamic_thread/src/main.c | 10 +++++-- 7 files changed, 32 insertions(+), 46 deletions(-) diff --git a/tests/arch/x86/static_idt/src/main.c b/tests/arch/x86/static_idt/src/main.c index b4360f123e1..82f1140b8fe 100644 --- a/tests/arch/x86/static_idt/src/main.c +++ b/tests/arch/x86/static_idt/src/main.c @@ -50,8 +50,14 @@ static volatile int spur_handler_aborted_thread = 1; void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *esf) { - zassert_equal(reason, K_ERR_SPURIOUS_IRQ, "wrong error reason"); - zassert_equal(k_current_get(), &my_thread, "wrong thread crashed"); + if (reason != K_ERR_SPURIOUS_IRQ) { + printk("wrong error reason\n"); + k_fatal_halt(reason); + } + if (k_current_get() != &my_thread) { + printk("wrong thread crashed\n"); + k_fatal_halt(reason); + } } /** diff --git a/tests/kernel/mem_protect/mem_protect/src/common.c b/tests/kernel/mem_protect/mem_protect/src/common.c index af68202d89c..98118045416 100644 --- a/tests/kernel/mem_protect/mem_protect/src/common.c +++ b/tests/kernel/mem_protect/mem_protect/src/common.c @@ -26,10 +26,6 @@ void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf) valid_fault = false; /* reset back to normal */ ztest_test_pass(); } else { - ztest_test_fail(); + k_fatal_halt(reason); } - -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) - CODE_UNREACHABLE; -#endif } diff --git a/tests/kernel/mem_protect/protection/src/main.c b/tests/kernel/mem_protect/protection/src/main.c index 56fc7257c43..e3057830d47 100644 --- a/tests/kernel/mem_protect/protection/src/main.c +++ b/tests/kernel/mem_protect/protection/src/main.c @@ -17,21 +17,10 @@ #define INFO(fmt, ...) printk(fmt, ##__VA_ARGS__) -/* ARM is a special case, in that k_thread_abort() does indeed return - * instead of calling z_swap() directly. The PendSV exception is queued - * and immediately fires upon completing the exception path; the faulting - * thread is never run again. - */ -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) -FUNC_NORETURN -#endif void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf) { INFO("Caught system error -- reason %d\n", reason); ztest_test_pass(); -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) - CODE_UNREACHABLE; -#endif } #ifdef CONFIG_CPU_CORTEX_M diff --git a/tests/kernel/mem_protect/stackprot/src/main.c b/tests/kernel/mem_protect/stackprot/src/main.c index 6f8886724e7..7958c152cc6 100644 --- a/tests/kernel/mem_protect/stackprot/src/main.c +++ b/tests/kernel/mem_protect/stackprot/src/main.c @@ -17,7 +17,10 @@ ZTEST_BMEM static int ret = TC_PASS; void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *esf) { - zassert_equal(reason, K_ERR_STACK_CHK_FAIL, "wrong error type"); + if (reason != K_ERR_STACK_CHK_FAIL) { + printk("wrong error type\n"); + k_fatal_halt(reason); + } } void check_input(const char *name, const char *input); diff --git a/tests/kernel/mem_protect/userspace/src/main.c b/tests/kernel/mem_protect/userspace/src/main.c index ccfe5bbcdcd..7806cae3592 100644 --- a/tests/kernel/mem_protect/userspace/src/main.c +++ b/tests/kernel/mem_protect/userspace/src/main.c @@ -66,37 +66,27 @@ K_APP_BMEM(part0) static volatile unsigned int expected_reason; */ #define BARRIER() k_sem_give(&expect_fault_sem) -/* ARM is a special case, in that k_thread_abort() does indeed return - * instead of calling z_swap() directly. The PendSV exception is queued - * and immediately fires upon completing the exception path; the faulting - * thread is never run again. - */ -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) -FUNC_NORETURN -#endif void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf) { INFO("Caught system error -- reason %d\n", reason); - /* - * If there is a user thread waiting for notification to exit, - * give it that notification. - */ - if (give_uthread_end_sem) { - give_uthread_end_sem = false; - k_sem_give(&uthread_end_sem); - } if (expect_fault && expected_reason == reason) { + /* + * If there is a user thread waiting for notification to exit, + * give it that notification. + */ + if (give_uthread_end_sem) { + give_uthread_end_sem = false; + k_sem_give(&uthread_end_sem); + } expect_fault = false; expected_reason = 0; BARRIER(); ztest_test_pass(); } else { - zassert_unreachable("Unexpected fault during test"); + printk("Unexpected fault during test"); + k_fatal_halt(reason); } -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) - CODE_UNREACHABLE; -#endif } /** diff --git a/tests/kernel/pipe/pipe/src/test_pipe.c b/tests/kernel/pipe/pipe/src/test_pipe.c index 6d3658c3c83..1b9efe7bca7 100644 --- a/tests/kernel/pipe/pipe/src/test_pipe.c +++ b/tests/kernel/pipe/pipe/src/test_pipe.c @@ -682,12 +682,8 @@ void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf) valid_fault = false; /* reset back to normal */ ztest_test_pass(); } else { - ztest_test_fail(); + k_fatal_halt(reason); } -#if !(defined(CONFIG_ARM) || defined(CONFIG_ARC)) - CODE_UNREACHABLE; -#endif - } /******************************************************************************/ /* Test case entry points */ diff --git a/tests/kernel/threads/dynamic_thread/src/main.c b/tests/kernel/threads/dynamic_thread/src/main.c index b35e0083274..ce2e5adedcf 100644 --- a/tests/kernel/threads/dynamic_thread/src/main.c +++ b/tests/kernel/threads/dynamic_thread/src/main.c @@ -17,8 +17,14 @@ static ZTEST_BMEM struct k_thread *dyn_thread; void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *esf) { - zassert_equal(reason, K_ERR_KERNEL_OOPS, "wrong error reason"); - zassert_equal(k_current_get(), dyn_thread, "wrong thread crashed"); + if (reason != K_ERR_KERNEL_OOPS) { + printk("wrong error reason\n"); + k_fatal_halt(reason); + } + if (k_current_get() != dyn_thread) { + printk("wrong thread crashed\n"); + k_fatal_halt(reason); + } } static void dyn_thread_entry(void *p1, void *p2, void *p3)