From 3f677222747656d38418499b31e068f80b51c607 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Wed, 11 Oct 2023 16:54:24 +0530 Subject: [PATCH] refactor(tools/test_apps): Move HAL tests for MPU to the `panic` test-app --- components/hal/test/CMakeLists.txt | 3 -- components/hal/test/test_mpu.c | 53 ------------------- tools/ci/check_copyright_ignore.txt | 1 - .../system/panic/main/include/test_panic.h | 2 + .../system/panic/main/test_app_main.c | 3 ++ .../test_apps/system/panic/main/test_panic.c | 30 +++++++++++ tools/test_apps/system/panic/pytest_panic.py | 14 +++++ 7 files changed, 49 insertions(+), 57 deletions(-) delete mode 100644 components/hal/test/CMakeLists.txt delete mode 100644 components/hal/test/test_mpu.c diff --git a/components/hal/test/CMakeLists.txt b/components/hal/test/CMakeLists.txt deleted file mode 100644 index a06f271e1a..0000000000 --- a/components/hal/test/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "${include_dirs}" - PRIV_REQUIRES cmock test_utils) diff --git a/components/hal/test/test_mpu.c b/components/hal/test/test_mpu.c deleted file mode 100644 index dec5847807..0000000000 --- a/components/hal/test/test_mpu.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include "unity.h" - -#include "esp_attr.h" - -#include "hal/mpu_hal.h" - -// TODO ESP32-C3 IDF-2375 -// LL still not implemented - -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2, ESP32C6, ESP32H2) -//IDF-5058 - -volatile static int RTC_NOINIT_ATTR access = 0; - -static void trigger_illegal_access(void) -{ - access = 0; - intptr_t addr = 0x80000000; // MPU region 4 - volatile int __attribute__((unused)) val = 0; - - // Marked as an illegal access region at startup in ESP32, ESP32S2. - // Make accessible temporarily. - mpu_hal_set_region_access(4, MPU_REGION_RW); - - val = *((int*) addr); - ++access; - TEST_ASSERT_EQUAL(1, access); - printf("Sucessfully accessed location %p\r\n", (void*)addr); - - // Make access to region illegal again. - mpu_hal_set_region_access(4, MPU_REGION_ILLEGAL); - ++access; - - // Since access to region is illegal, this should fail (causing a reset), and the increment - // to access count is not performed. - val = *((int*) addr); - ++access; -} - -void check_access(void) -{ - TEST_ASSERT_EQUAL(2, access); -} - -TEST_CASE_MULTIPLE_STAGES("Can set illegal access regions", "[soc][mpu]", - trigger_illegal_access, - check_access); - -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(...) -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 3caf4d5646..1931e22e9c 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -546,7 +546,6 @@ components/freertos/FreeRTOS-Kernel-SMP/timers.c components/hal/include/hal/dac_types.h components/hal/spi_slave_hal.c components/hal/spi_slave_hal_iram.c -components/hal/test/test_mpu.c components/heap/test_multi_heap_host/main.cpp components/heap/test_multi_heap_host/test_multi_heap.cpp components/idf_test/include/idf_performance.h diff --git a/tools/test_apps/system/panic/main/include/test_panic.h b/tools/test_apps/system/panic/main/include/test_panic.h index 1121a62648..6b681a4f5e 100644 --- a/tools/test_apps/system/panic/main/include/test_panic.h +++ b/tools/test_apps/system/panic/main/include/test_panic.h @@ -53,6 +53,8 @@ void test_assert(void); void test_assert_cache_disabled(void); +void test_illegal_access(void); + #ifdef __cplusplus } #endif 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 d97d734173..ac4120d7b6 100644 --- a/tools/test_apps/system/panic/main/test_app_main.c +++ b/tools/test_apps/system/panic/main/test_app_main.c @@ -100,6 +100,9 @@ void app_main(void) HANDLE_TEST(test_name, test_ub); HANDLE_TEST(test_name, test_assert); HANDLE_TEST(test_name, test_assert_cache_disabled); +#if CONFIG_IDF_TARGET_ESP32 + HANDLE_TEST(test_name, test_illegal_access); +#endif #if CONFIG_TEST_MEMPROT diff --git a/tools/test_apps/system/panic/main/test_panic.c b/tools/test_apps/system/panic/main/test_panic.c index 9c33b74cca..655f9bd42a 100644 --- a/tools/test_apps/system/panic/main/test_panic.c +++ b/tools/test_apps/system/panic/main/test_panic.c @@ -19,6 +19,8 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "hal/mpu_hal.h" + /* Test utility function */ extern void esp_restart_noos(void) __attribute__ ((noreturn)); @@ -202,3 +204,31 @@ void test_ub(void) uint8_t stuff[1] = {rand()}; printf("%d\n", stuff[rand()]); } + +/* NOTE: The following test verifies the behaviour for the + * Xtensa-specific MPU instructions (Refer WDTLB, DSYNC, WDTIB, ISYNC) + * used for memory protection. + * + * However, this test is not valid for S2 and S3, because they have PMS + * enabled on top of this, giving unpredicatable results. + */ +#if CONFIG_IDF_TARGET_ESP32 +void test_illegal_access(void) +{ + intptr_t addr = 0x80000000; // MPU region 4 + volatile int __attribute__((unused)) val = INT16_MAX; + + // Marked as an illegal access region at startup in ESP32, ESP32S2. + // Make accessible temporarily. + mpu_hal_set_region_access(4, MPU_REGION_RW); + + val = *((int*) addr); + printf("[1] val: %d at %p\n", val, (void *)addr); + + // Make access to region illegal again. + mpu_hal_set_region_access(4, MPU_REGION_ILLEGAL); + val = *((int*) addr); + // Does not reach here as device resets due to illegal access + printf("[2] val: %d at %p\n", val, (void *)addr); +} +#endif diff --git a/tools/test_apps/system/panic/pytest_panic.py b/tools/test_apps/system/panic/pytest_panic.py index e1de4a805c..3f110d4e01 100644 --- a/tools/test_apps/system/panic/pytest_panic.py +++ b/tools/test_apps/system/panic/pytest_panic.py @@ -822,3 +822,17 @@ def test_hw_stack_guard_cpu0(dut: PanicTestDut, config: str, test_func_name: str dut.expect_exact('Stack pointer: 0x') dut.expect(r'Stack bounds: 0x(.*) - 0x') common_test(dut, config) + + +@pytest.mark.esp32 +@pytest.mark.parametrize('config', ['panic'], indirect=True) +@pytest.mark.generic +def test_illegal_access(dut: PanicTestDut, config: str, test_func_name: str) -> None: + dut.run_test_func(test_func_name) + if dut.is_xtensa: + dut.expect(r'\[1\] val: (-?\d+) at 0x80000000', timeout=30) + dut.expect_gme('LoadProhibited') + dut.expect_reg_dump(0) + dut.expect_backtrace() + dut.expect_elf_sha256() + dut.expect_none('Guru Meditation')