Files
esp-idf/tools/test_apps/system/panic/main/test_panic.c
T
Sudeep Mohanty 68277df469 fix(panic_handler): Updated panic handler to use RTC WDT
This commit updates the following:
- Updates the panic handler to use only the RTC WDT to reset the system.
- Refactors some of the panic handler code.
- Updates Bluetooth files where in they now feed the WDTs instead of
  reconfiguring them.
- Removes some unnecessary configuration of WDTs from various files.
- Added a unit test to verify that the system does not lock up when the
  panic handler is stuck.
- Updates the memprot unit tests to work with the refactored panic
  handler.

Closes https://github.com/espressif/esp-idf/issues/15166
Closes https://github.com/espressif/esp-idf/issues/15018
Closes https://github.com/espressif/esp-idf/issues/10110
2025-02-26 08:38:05 +01:00

230 lines
4.9 KiB
C

/*
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include "esp_partition.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "esp_private/cache_utils.h"
#include "esp_memory_utils.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
volatile uint32_t g_panic_handler_stuck = 0;
volatile uint32_t g_panic_handler_crash = 0;
/* Test utility function */
extern void esp_restart_noos(void) __attribute__ ((noreturn));
void die(const char* msg)
{
printf("Test error: %s\n\n", msg);
fflush(stdout);
usleep(1000);
/* Don't use abort here as it would enter the panic handler */
esp_restart_noos();
}
/* implementations of the test functions */
void test_abort(void)
{
abort();
}
void IRAM_ATTR test_abort_cache_disabled(void)
{
spi_flash_disable_interrupts_caches_and_other_cpu();
abort();
}
void test_int_wdt(void)
{
portDISABLE_INTERRUPTS();
while (true) {
;
}
}
void test_task_wdt_cpu0(void)
{
while (true) {
;
}
}
void __attribute__((no_sanitize_undefined)) test_panic_handler_stuck(void *arg)
{
g_panic_handler_stuck = 1;
/* Cause a panic */
#ifdef __XTENSA__
asm("ill"); // should be an invalid operation on xtensa targets
#elif __riscv
asm("unimp"); // should be an invalid operation on RISC-V targets
#endif
vTaskDelete(NULL);
}
void __attribute__((no_sanitize_undefined)) test_panic_handler_crash(void *arg)
{
g_panic_handler_crash = 1;
/* Cause a panic */
#ifdef __XTENSA__
asm("ill");
#elif __riscv
asm("unimp");
#endif
vTaskDelete(NULL);
}
#if !CONFIG_FREERTOS_UNICORE
static void infinite_loop(void* arg) {
(void) arg;
while(1) {
;
}
}
void test_task_wdt_cpu1(void)
{
xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 1, NULL, 1);
while (true) {
vTaskDelay(1);
}
}
void test_panic_handler_stuck1(void)
{
/* Cause the panic on core 1 */
xTaskCreatePinnedToCore(test_panic_handler_stuck, "panic_handler_stuck", 2048, NULL, 1, NULL, 1);
while(1) {
vTaskDelay(10);
}
}
void test_panic_handler_crash1(void)
{
/* Cause the panic on core 1 */
xTaskCreatePinnedToCore(test_panic_handler_crash, "panic_handler_crash", 2048, NULL, 1, NULL, 1);
while(1) {
vTaskDelay(10);
}
}
#endif
void test_panic_handler_stuck0(void)
{
/* Cause the panic on core 0 */
xTaskCreatePinnedToCore(test_panic_handler_stuck, "panic_handler_stuck", 2048, NULL, 1, NULL, 0);
while(1) {
vTaskDelay(10);
}
}
void test_panic_handler_crash0(void)
{
/* Cause the panic on core 0 */
xTaskCreatePinnedToCore(test_panic_handler_crash, "panic_handler_crash", 2048, NULL, 1, NULL, 0);
while(1) {
vTaskDelay(10);
}
}
void __attribute__((no_sanitize_undefined)) test_storeprohibited(void)
{
*(int*) 0x1 = 0;
}
void IRAM_ATTR test_cache_error(void)
{
spi_flash_disable_interrupts_caches_and_other_cpu();
die("this should not be printed");
}
void IRAM_ATTR test_int_wdt_cache_disabled(void)
{
spi_flash_disable_interrupts_caches_and_other_cpu();
portDISABLE_INTERRUPTS();
while (true) {
;
}
}
void test_assert(void)
{
assert(0);
}
void IRAM_ATTR test_assert_cache_disabled(void)
{
spi_flash_disable_interrupts_caches_and_other_cpu();
assert(0);
}
/**
* This function overwrites the stack beginning from the valid area continuously towards and beyond
* the end of the stack (stack base) of the current task.
* This is to test stack protection measures like a watchpoint at the end of the stack.
*
* @note: This test DOES NOT write beyond the stack limit. It only writes up to exactly the limit itself.
* The FreeRTOS stack protection mechanisms all trigger shortly before the end of the stack.
*/
void test_stack_overflow(void)
{
register uint32_t* sp asm("sp");
TaskStatus_t pxTaskStatus;
vTaskGetInfo(NULL, &pxTaskStatus, pdFALSE, pdFALSE);
uint32_t *end = (uint32_t*) pxTaskStatus.pxStackBase;
// offset - 20 bytes from SP in order to not corrupt the current frame.
// Need to write from higher to lower addresses since the stack grows downwards and the watchpoint/canary is near
// the end of the stack (lowest address).
for (uint32_t* ptr = sp - 5; ptr != end; --ptr) {
*ptr = 0;
}
// trigger a context switch to initiate checking the FreeRTOS stack canary
vTaskDelay(pdMS_TO_TICKS(0));
}
void test_illegal_instruction(void)
{
#if __XTENSA__
__asm__ __volatile__("ill");
#elif __riscv
__asm__ __volatile__("unimp");
#endif
}
void test_instr_fetch_prohibited(void)
{
typedef void (*fptr_t)(void);
volatile fptr_t fptr = (fptr_t) 0x4;
fptr();
}
void test_ub(void)
{
uint8_t stuff[1] = {rand()};
printf("%d\n", stuff[rand()]);
}