Skip to content

Commit dea3651

Browse files
fsammoura1980Hardevsinh-Palaniya
authored andcommitted
tests: riscv: Validate PMP stack protection under MULTITHREADING
The PMP stack guard test was previously limited to the non-multithreaded (no-mt) configuration. This commit updates the test implementation to conditionally handle stack boundary lookup: 1. When CONFIG_MULTITHREADING is enabled, it uses thread-specific stack information (e.g., z_interrupt_stacks[_current_cpu->id] for ISR stack and k_current_get()->stack_info for the main thread stack). 2. New test configurations (arch.riscv.pmp.mt.*) are added to explicitly run the tests with CONFIG_MULTITHREADING=y. This ensures PMP stack guards are correctly verified in a multi-threaded environment. Signed-off-by: Firas Sammoura <[email protected]>
1 parent 9c18186 commit dea3651

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y

tests/arch/riscv/pmp/no-multithreading/src/main.c renamed to tests/arch/riscv/pmp/isr-stack-guard/src/main.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
2424
}
2525

2626
#ifdef CONFIG_PMP_STACK_GUARD
27+
28+
#ifdef CONFIG_MULTITHREADING
29+
30+
static void check_isr_stack_guard(void)
31+
{
32+
char *isr_stack = (char *)z_interrupt_stacks[_current_cpu->id];
33+
34+
valid_fault = true;
35+
*isr_stack = 42;
36+
}
37+
38+
static void check_main_stack_guard(void)
39+
{
40+
struct k_thread *current_thread_ptr = k_current_get();
41+
uintptr_t stack_bottom = current_thread_ptr->stack_info.start - K_KERNEL_STACK_RESERVED;
42+
43+
char *main_stack = (char *)stack_bottom;
44+
45+
valid_fault = true;
46+
*main_stack = 42;
47+
}
48+
49+
#else /* CONFIG_MULTITHREADING */
50+
2751
static void check_isr_stack_guard(void)
2852
{
2953
char *isr_stack = (char *)z_interrupt_stacks;
@@ -40,7 +64,9 @@ static void check_main_stack_guard(void)
4064
*main_stack = 42;
4165
}
4266

43-
#else
67+
#endif /* CONFIG_MULTITHREADING */
68+
69+
#else /* CONFIG_PMP_STACK_GUARD */
4470

4571
static void check_isr_stack_guard(void)
4672
{
@@ -65,7 +91,7 @@ static const pmp_test_func_t pmp_test_func[] = {
6591
* @brief Verify RISC-V specific PMP stack guard regions.
6692
* @details Manually write to the protected stack region to trigger fatal error.
6793
*/
68-
ZTEST(riscv_pmp_no_mt, test_pmp)
94+
ZTEST(riscv_pmp_isr_main_stack, test_pmp)
6995
{
7096
#ifndef PMP_TEST_FUNC_IDX
7197
#define PMP_TEST_FUNC_IDX 0
@@ -76,4 +102,4 @@ ZTEST(riscv_pmp_no_mt, test_pmp)
76102
TC_END_REPORT(TC_FAIL);
77103
}
78104

79-
ZTEST_SUITE(riscv_pmp_no_mt, NULL, NULL, NULL, NULL, NULL);
105+
ZTEST_SUITE(riscv_pmp_isr_main_stack, NULL, NULL, NULL, NULL, NULL);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
common:
2+
platform_allow:
3+
- qemu_riscv32
4+
- qemu_riscv32e
5+
- qemu_riscv64
6+
filter: CONFIG_RISCV_PMP
7+
ignore_faults: true
8+
9+
tests:
10+
arch.riscv.pmp.no-mt.isr-stack-guard:
11+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=0
12+
extra_configs:
13+
- CONFIG_MULTITHREADING=n
14+
arch.riscv.pmp.no-mt.main-stack-guard:
15+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=1
16+
extra_configs:
17+
- CONFIG_MULTITHREADING=n
18+
arch.riscv.pmp.mt.isr-stack-guard:
19+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=0
20+
extra_configs:
21+
- CONFIG_MULTITHREADING=y
22+
arch.riscv.pmp.mt.main-stack-guard:
23+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=1
24+
extra_configs:
25+
- CONFIG_MULTITHREADING=y

tests/arch/riscv/pmp/no-multithreading/prj.conf

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/arch/riscv/pmp/no-multithreading/testcase.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)