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 
k_sys_fatal_error_handler(unsigned int reason,const struct arch_esf * pEsf)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 
check_null_ptr_guard(void)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 
ZTEST(riscv_pmp_null_pointer,test_null_pointer_access)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);
50