Skip to content

Commit 7a9154e

Browse files
wearyzengithub-actions[bot]
authored andcommitted
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 <sudan.landge@arm.com> (cherry picked from commit a55053b)
1 parent 9bacf34 commit 7a9154e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

boards/arm/mps2/Kconfig.defconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ config TEST_EXTRA_STACK_SIZE
3333

3434
endif # COVERAGE_GCOV
3535

36-
endif
36+
endif # BOARD_MPS2_AN383 || BOARD_MPS2_AN385 || BOARD_MPS2_AN386 || BOARD_MPS2_AN500
3737

3838
if BOARD_MPS2_AN521_CPU0 || BOARD_MPS2_AN521_CPU0_NS || BOARD_MPS2_AN521_CPU1
3939

@@ -58,4 +58,11 @@ config UART_INTERRUPT_DRIVEN
5858

5959
endif # SERIAL
6060

61+
endif # BOARD_MPS2_AN521_CPU0 || BOARD_MPS2_AN521_CPU0_NS || BOARD_MPS2_AN521_CPU1
62+
63+
if QEMU_TARGET
64+
65+
config ISR_STACK_SIZE
66+
default 4096
67+
6168
endif

tests/arch/arm/arm_user_stack_test/src/main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ volatile int *const attack_sp = &attack_stack[128];
2323
const int sysno = K_SYSCALL_K_UPTIME_TICKS;
2424
k_tid_t low_tid, hi_tid;
2525

26+
struct k_timer timer;
27+
volatile ZTEST_BMEM uint64_t hi_thread_runs, test_completed;
28+
2629
void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
2730
{
31+
test_completed = 1;
32+
k_timer_stop(&timer);
2833
ztest_test_pass();
2934
k_thread_abort(low_tid);
3035

@@ -37,6 +42,24 @@ void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
3742
}
3843
}
3944

45+
static void timeout_handler(struct k_timer *timer)
46+
{
47+
if (!test_completed) {
48+
49+
printf("hi_thread_runs: %lld\n", hi_thread_runs);
50+
/* the timer times out after 120s,
51+
* by then hi_fn would have ran multiple times so
52+
* compare against a random number like 1000 to make sure that
53+
* hi_fn actually ran for a while
54+
*/
55+
if (hi_thread_runs > 1000) {
56+
ztest_test_pass();
57+
} else {
58+
ztest_test_fail();
59+
}
60+
}
61+
}
62+
4063
void attack_entry(void)
4164
{
4265
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)
79102
while (1) {
80103
attack_sp[-2] = (int)attack_entry;
81104
k_msleep(1);
105+
hi_thread_runs++;
82106
}
83107
}
84108

85109
ZTEST(arm_user_stack_test, test_arm_user_stack_corruption)
86110
{
111+
k_timer_init(&timer, timeout_handler, NULL);
112+
k_timer_start(&timer, K_SECONDS(120), K_NO_WAIT);
113+
87114
low_tid = k_thread_create(&th0, stk0, K_THREAD_STACK_SIZEOF(stk0), low_fn, NULL, NULL, NULL,
88115
2,
89116
#ifdef CONFIG_FPU_SHARING

tests/arch/arm/arm_user_stack_test/testcase.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
common:
22
tags:
33
- arm
4+
timeout: 120
45
tests:
56
arch.arm.user.stack:
67
filter: CONFIG_CPU_CORTEX_M

0 commit comments

Comments
 (0)