Skip to content

Commit c515733

Browse files
committed
tests: riscv: pmp: Add test case for null pointer access
Adds `test_null_pointer_access` to verify that attempting to write to a null pointer (address 0x0) correctly triggers a fatal error, confirming the Physical Memory Protection (PMP) guard functionality. Signed-off-by: Firas Sammoura <[email protected]>
1 parent 7c39469 commit c515733

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(riscv_pmp)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
target_include_directories(app PRIVATE
11+
${ZEPHYR_BASE}/kernel/include
12+
${ZEPHYR_BASE}/arch/${ARCH}/include
13+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_NULL_POINTER_EXCEPTION_DETECTION_PMP=y
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2025 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <kernel_internal.h>
8+
#include <zephyr/tc_util.h>
9+
#include <zephyr/ztest.h>
10+
#include <zephyr/arch/riscv/pmp.h>
11+
12+
ZTEST_BMEM volatile bool valid_fault;
13+
14+
void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
15+
{
16+
printk("Caught system error -- reason %d %d\n", reason, valid_fault);
17+
if (!valid_fault) {
18+
printk("fatal error was unexpected, aborting\n");
19+
TC_END_REPORT(TC_FAIL);
20+
k_fatal_halt(reason);
21+
}
22+
23+
printk("fatal error expected as part of test case\n");
24+
valid_fault = false; /* reset back to normal */
25+
TC_END_REPORT(TC_PASS);
26+
}
27+
28+
static void check_null_ptr_guard(void)
29+
{
30+
volatile int *null_ptr = NULL;
31+
32+
valid_fault = true;
33+
*null_ptr = 42;
34+
35+
if (valid_fault) {
36+
/*
37+
* valid_fault gets cleared if an expected exception took place
38+
*/
39+
printk("test function was supposed to fault but didn't\n");
40+
ztest_test_fail();
41+
}
42+
}
43+
44+
ZTEST(riscv_pmp_null_pointer, test_null_pointer_access)
45+
{
46+
check_null_ptr_guard();
47+
48+
zassert_unreachable("Write to stack guard did not fault");
49+
TC_END_REPORT(TC_FAIL);
50+
}
51+
52+
ZTEST_SUITE(riscv_pmp_null_pointer, NULL, NULL, NULL, NULL, NULL);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
common:
2+
platform_allow:
3+
- qemu_riscv32
4+
- qemu_riscv32e
5+
- qemu_riscv64
6+
filter: CONFIG_RISCV_PMP
7+
8+
tests:
9+
arch.riscv.pmp.null_pointer.locked: {}
10+
arch.riscv.pmp.null_pointer.unlocked:
11+
extra_configs:
12+
- CONFIG_PMP_NO_LOCK_GLOBAL=y

0 commit comments

Comments
 (0)