Skip to content

Commit 41403ca

Browse files
fsammoura1980nashif
authored andcommitted
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 3fbaa29 commit 41403ca

File tree

4 files changed

+67
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)