From 2265c0f230fb1a15b4b0c0eaa6fcd7329052f335 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Mon, 5 Feb 2024 17:18:44 +0530 Subject: [PATCH] feat(tools/test_apps): Add violation tests for the flash I/DROM region - For SoCs supporting PMP --- .../system/panic/main/include/test_memprot.h | 7 +++- .../system/panic/main/test_app_main.c | 8 +++- .../system/panic/main/test_memprot.c | 37 +++++++++++++++- tools/test_apps/system/panic/pytest_panic.py | 42 +++++++++++++++++-- 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/tools/test_apps/system/panic/main/include/test_memprot.h b/tools/test_apps/system/panic/main/include/test_memprot.h index c4b229a8b9..d21d795f60 100644 --- a/tools/test_apps/system/panic/main/include/test_memprot.h +++ b/tools/test_apps/system/panic/main/include/test_memprot.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -42,6 +42,11 @@ void test_rtc_slow_reg1_execute_violation(void); void test_rtc_slow_reg2_execute_violation(void); +void test_irom_reg_write_violation(void); + +void test_drom_reg_write_violation(void); + +void test_drom_reg_execute_violation(void); #ifdef __cplusplus } diff --git a/tools/test_apps/system/panic/main/test_app_main.c b/tools/test_apps/system/panic/main/test_app_main.c index 1d1f0c1cdc..aecd9f0836 100644 --- a/tools/test_apps/system/panic/main/test_app_main.c +++ b/tools/test_apps/system/panic/main/test_app_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -137,6 +137,12 @@ void app_main(void) HANDLE_TEST(test_name, test_rtc_slow_reg2_execute_violation); #endif +#if CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT + HANDLE_TEST(test_name, test_irom_reg_write_violation); + HANDLE_TEST(test_name, test_drom_reg_write_violation); + HANDLE_TEST(test_name, test_drom_reg_execute_violation); +#endif + #endif die("Unknown test name"); diff --git a/tools/test_apps/system/panic/main/test_memprot.c b/tools/test_apps/system/panic/main/test_memprot.c index 3c559f1fc1..cfefa415e6 100644 --- a/tools/test_apps/system/panic/main/test_memprot.c +++ b/tools/test_apps/system/panic/main/test_memprot.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,7 +15,7 @@ #include "soc/soc.h" #include "test_memprot.h" - +#include "sdkconfig.h" #define RND_VAL (0xA5A5A5A5) #define SPIN_ITER (16) @@ -213,3 +213,36 @@ static void __attribute__((constructor)) test_print_rtc_var_func(void) printf("foo_s: %p | var_s: %p\n", &foo_s, &var_s); #endif } + + +/* ---------------------------------------------------- I/D Cache (Flash) Violation Checks ---------------------------------------------------- */ + +#if CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT +static const uint16_t foo_buf[8] = { + 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, +}; + +void test_irom_reg_write_violation(void) +{ + extern int _instruction_reserved_end; + uint32_t *test_addr = (uint32_t *)((uint32_t)(&_instruction_reserved_end - 0x100)); + printf("Flash (IROM): Write operation | Address: %p\n", test_addr); + *test_addr = RND_VAL; +} + +void test_drom_reg_write_violation(void) +{ + uint32_t *test_addr = (uint32_t *)((uint32_t)(foo_buf)); + printf("Flash (DROM): Write operation | Address: %p\n", test_addr); + *test_addr = RND_VAL; +} + +void test_drom_reg_execute_violation(void) +{ + printf("Flash (DROM): Execute operation | Address: %p\n", foo_buf); + void (*func_ptr)(void); + func_ptr = (void(*)(void))foo_buf; + func_ptr(); +} +#endif diff --git a/tools/test_apps/system/panic/pytest_panic.py b/tools/test_apps/system/panic/pytest_panic.py index d96b20f200..08da655338 100644 --- a/tools/test_apps/system/panic/pytest_panic.py +++ b/tools/test_apps/system/panic/pytest_panic.py @@ -1,8 +1,9 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 - import re -from typing import List, Optional, Union +from typing import List +from typing import Optional +from typing import Union import pexpect import pytest @@ -544,6 +545,11 @@ CONFIGS_MEMPROT_RTC_SLOW_MEM = [ pytest.param('memprot_esp32s2', marks=[pytest.mark.esp32s2]), ] +CONFIGS_MEMPROT_FLASH_IDROM = [ + pytest.param('memprot_esp32c6', marks=[pytest.mark.esp32c6]), + pytest.param('memprot_esp32h2', marks=[pytest.mark.esp32h2]) +] + @pytest.mark.parametrize('config', CONFIGS_MEMPROT_DCACHE, indirect=True) @pytest.mark.generic @@ -780,6 +786,36 @@ def test_rtc_slow_reg2_execute_violation(dut: PanicTestDut, test_func_name: str) dut.expect_cpu_reset() +@pytest.mark.parametrize('config', CONFIGS_MEMPROT_FLASH_IDROM, indirect=True) +@pytest.mark.generic +def test_irom_reg_write_violation(dut: PanicTestDut, test_func_name: str) -> None: + dut.run_test_func(test_func_name) + if dut.target == 'esp32c6': + dut.expect_gme('Store access fault') + elif dut.target == 'esp32h2': + dut.expect_gme('Cache error') + dut.expect_reg_dump(0) + dut.expect_cpu_reset() + + +@pytest.mark.parametrize('config', CONFIGS_MEMPROT_FLASH_IDROM, indirect=True) +@pytest.mark.generic +def test_drom_reg_write_violation(dut: PanicTestDut, test_func_name: str) -> None: + dut.run_test_func(test_func_name) + dut.expect_gme('Store access fault') + dut.expect_reg_dump(0) + dut.expect_cpu_reset() + + +@pytest.mark.parametrize('config', CONFIGS_MEMPROT_FLASH_IDROM, indirect=True) +@pytest.mark.generic +def test_drom_reg_execute_violation(dut: PanicTestDut, test_func_name: str) -> None: + dut.run_test_func(test_func_name) + dut.expect_gme('Instruction access fault') + dut.expect_reg_dump(0) + dut.expect_cpu_reset() + + @pytest.mark.esp32 @pytest.mark.parametrize('config', ['gdbstub_coredump'], indirect=True) def test_gdbstub_coredump(dut: PanicTestDut) -> None: