Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/arch/riscv/pmp/null-pointer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(riscv_pmp)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
2 changes: 2 additions & 0 deletions tests/arch/riscv/pmp/null-pointer/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_NULL_POINTER_EXCEPTION_DETECTION_PMP=y
49 changes: 49 additions & 0 deletions tests/arch/riscv/pmp/null-pointer/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>

static volatile bool valid_fault;

void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
{
TC_PRINT("Caught system error -- reason %d %d\n", reason, valid_fault);
if (!valid_fault) {
TC_PRINT("fatal error was unexpected, aborting\n");
TC_END_REPORT(TC_FAIL);
k_fatal_halt(reason);
}

TC_PRINT("fatal error expected as part of test case\n");
valid_fault = false; /* reset back to normal */
TC_END_REPORT(TC_PASS);
}

static void check_null_ptr_guard(void)
{
volatile int *null_ptr = NULL;

valid_fault = true;
*null_ptr = 42;

Check failure on line 30 in tests/arch/riscv/pmp/null-pointer/src/main.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Dereference of null pointer (loaded from variable 'null_ptr')

See more on https://sonarcloud.io/project/issues?id=zephyrproject-rtos_zephyr&issues=AZsEDw-EVJgy9hJZgzcz&open=AZsEDw-EVJgy9hJZgzcz&pullRequest=100761

if (valid_fault) {
/*
* valid_fault gets cleared if an expected exception took place
*/
TC_PRINT("test function was supposed to fault but didn't\n");
ztest_test_fail();
}
}

ZTEST(riscv_pmp_null_pointer, test_null_pointer_access)
{
check_null_ptr_guard();

zassert_unreachable("Write to stack guard did not fault");
TC_END_REPORT(TC_FAIL);
}

ZTEST_SUITE(riscv_pmp_null_pointer, NULL, NULL, NULL, NULL, NULL);
8 changes: 8 additions & 0 deletions tests/arch/riscv/pmp/null-pointer/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
common:
filter: CONFIG_RISCV_PMP

tests:
arch.riscv.pmp.null_pointer.locked: {}
arch.riscv.pmp.null_pointer.unlocked:
extra_configs:
- CONFIG_PMP_NO_LOCK_GLOBAL=y