From 7a9154e0574555c2527f0d52c528792ecb673a09 Mon Sep 17 00:00:00 2001 From: Sudan Landge Date: Thu, 16 Oct 2025 12:08:02 +0100 Subject: [PATCH 1/2] tests: fix arch.arm.user.stack test failure Fixes #97473 by: - Marking the test as passed if the hardware executes user threads for a while without triggering a stack corruption, instead of waiting indefinitely. - Adding a timeout to ensure the test exists gracefully if the issue is not reproduced. Also fixes a stack corruption issue on QEMU targets, caused by insufficient stack size and revealed by the timer change. Signed-off-by: Sudan Landge (cherry picked from commit a55053b2164a3f6742ac57d33769ad85288ae56e) --- boards/arm/mps2/Kconfig.defconfig | 9 ++++++- tests/arch/arm/arm_user_stack_test/src/main.c | 27 +++++++++++++++++++ .../arm/arm_user_stack_test/testcase.yaml | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/boards/arm/mps2/Kconfig.defconfig b/boards/arm/mps2/Kconfig.defconfig index 367e09c2a9a17..b6c51af651c41 100644 --- a/boards/arm/mps2/Kconfig.defconfig +++ b/boards/arm/mps2/Kconfig.defconfig @@ -33,7 +33,7 @@ config TEST_EXTRA_STACK_SIZE endif # COVERAGE_GCOV -endif +endif # BOARD_MPS2_AN383 || BOARD_MPS2_AN385 || BOARD_MPS2_AN386 || BOARD_MPS2_AN500 if BOARD_MPS2_AN521_CPU0 || BOARD_MPS2_AN521_CPU0_NS || BOARD_MPS2_AN521_CPU1 @@ -58,4 +58,11 @@ config UART_INTERRUPT_DRIVEN endif # SERIAL +endif # BOARD_MPS2_AN521_CPU0 || BOARD_MPS2_AN521_CPU0_NS || BOARD_MPS2_AN521_CPU1 + +if QEMU_TARGET + +config ISR_STACK_SIZE + default 4096 + endif diff --git a/tests/arch/arm/arm_user_stack_test/src/main.c b/tests/arch/arm/arm_user_stack_test/src/main.c index ee7e1ed90bbc1..71d9316aec358 100644 --- a/tests/arch/arm/arm_user_stack_test/src/main.c +++ b/tests/arch/arm/arm_user_stack_test/src/main.c @@ -23,8 +23,13 @@ volatile int *const attack_sp = &attack_stack[128]; const int sysno = K_SYSCALL_K_UPTIME_TICKS; k_tid_t low_tid, hi_tid; +struct k_timer timer; +volatile ZTEST_BMEM uint64_t hi_thread_runs, test_completed; + void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf) { + test_completed = 1; + k_timer_stop(&timer); ztest_test_pass(); k_thread_abort(low_tid); @@ -37,6 +42,24 @@ void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf) } } +static void timeout_handler(struct k_timer *timer) +{ + if (!test_completed) { + + printf("hi_thread_runs: %lld\n", hi_thread_runs); + /* the timer times out after 120s, + * by then hi_fn would have ran multiple times so + * compare against a random number like 1000 to make sure that + * hi_fn actually ran for a while + */ + if (hi_thread_runs > 1000) { + ztest_test_pass(); + } else { + ztest_test_fail(); + } + } +} + void attack_entry(void) { printf("Call %s from %s\n", __func__, k_is_user_context() ? "user" : "kernel"); @@ -79,11 +102,15 @@ void hi_fn(void *arg1, void *arg2, void *arg3) while (1) { attack_sp[-2] = (int)attack_entry; k_msleep(1); + hi_thread_runs++; } } ZTEST(arm_user_stack_test, test_arm_user_stack_corruption) { + k_timer_init(&timer, timeout_handler, NULL); + k_timer_start(&timer, K_SECONDS(120), K_NO_WAIT); + low_tid = k_thread_create(&th0, stk0, K_THREAD_STACK_SIZEOF(stk0), low_fn, NULL, NULL, NULL, 2, #ifdef CONFIG_FPU_SHARING diff --git a/tests/arch/arm/arm_user_stack_test/testcase.yaml b/tests/arch/arm/arm_user_stack_test/testcase.yaml index 49cd8fd3a00e8..0d7bafb8fa124 100644 --- a/tests/arch/arm/arm_user_stack_test/testcase.yaml +++ b/tests/arch/arm/arm_user_stack_test/testcase.yaml @@ -1,6 +1,7 @@ common: tags: - arm + timeout: 120 tests: arch.arm.user.stack: filter: CONFIG_CPU_CORTEX_M From fb0c868de654400787bf27af7f375d84e7f0da15 Mon Sep 17 00:00:00 2001 From: Sudan Landge Date: Wed, 29 Oct 2025 15:50:11 +0000 Subject: [PATCH 2/2] tests: arm: exclude mps2/an385 on user stack test Temporarily disable running the user stack tests on mps2/an385 to unblock ci issues reported in #98494. Signed-off-by: Sudan Landge (cherry picked from commit 2408b808d2d8429908e31fea0933dbd844ba1a08) --- tests/arch/arm/arm_user_stack_test/testcase.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/arch/arm/arm_user_stack_test/testcase.yaml b/tests/arch/arm/arm_user_stack_test/testcase.yaml index 0d7bafb8fa124..05cd98993ff91 100644 --- a/tests/arch/arm/arm_user_stack_test/testcase.yaml +++ b/tests/arch/arm/arm_user_stack_test/testcase.yaml @@ -2,6 +2,9 @@ common: tags: - arm timeout: 120 + # TODO: remove the platform_exclude once the issue with MPS2/AN385 is fixed. + platform_exclude: + - mps2/an385 tests: arch.arm.user.stack: filter: CONFIG_CPU_CORTEX_M