test_panic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #include "esp_partition.h"
  11. #include "esp_flash.h"
  12. #include "esp_system.h"
  13. #include "esp_private/cache_utils.h"
  14. #include "esp_memory_utils.h"
  15. #include "esp_heap_caps.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. /* Test utility function */
  19. extern void esp_restart_noos(void) __attribute__ ((noreturn));
  20. void die(const char* msg)
  21. {
  22. printf("Test error: %s\n\n", msg);
  23. fflush(stdout);
  24. usleep(1000);
  25. /* Don't use abort here as it would enter the panic handler */
  26. esp_restart_noos();
  27. }
  28. /* implementations of the test functions */
  29. void test_abort(void)
  30. {
  31. abort();
  32. }
  33. void IRAM_ATTR test_abort_cache_disabled(void)
  34. {
  35. spi_flash_disable_interrupts_caches_and_other_cpu();
  36. abort();
  37. }
  38. void test_int_wdt(void)
  39. {
  40. portDISABLE_INTERRUPTS();
  41. while (true) {
  42. ;
  43. }
  44. }
  45. void test_task_wdt_cpu0(void)
  46. {
  47. while (true) {
  48. ;
  49. }
  50. }
  51. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  52. static void stack_in_extram(void* arg) {
  53. (void) arg;
  54. /* Abort instead of using a load/store prohibited to prevent a sanitize error */
  55. abort();
  56. }
  57. void test_panic_extram_stack(void) {
  58. /* Start by initializing a Task which has a stack in external RAM */
  59. StaticTask_t handle;
  60. const uint32_t stack_size = 8192;
  61. void* stack = heap_caps_malloc(stack_size, MALLOC_CAP_SPIRAM);
  62. /* Make sure the stack is in external RAM */
  63. if (!esp_ptr_external_ram(stack)) {
  64. die("Allocated stack is not in external RAM!\n");
  65. }
  66. xTaskCreateStatic(stack_in_extram, "Task_stack_extram", stack_size, NULL, 4, (StackType_t*) stack, &handle);
  67. vTaskDelay(1000);
  68. }
  69. #endif // ESP_COREDUMP_ENABLE_TO_FLASH && SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  70. #if !CONFIG_FREERTOS_UNICORE
  71. static void infinite_loop(void* arg) {
  72. (void) arg;
  73. while(1) {
  74. ;
  75. }
  76. }
  77. void test_task_wdt_cpu1(void)
  78. {
  79. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 1, NULL, 1);
  80. while (true) {
  81. vTaskDelay(1);
  82. }
  83. }
  84. void test_task_wdt_both_cpus(void)
  85. {
  86. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 4, NULL, 1);
  87. /* Give some time to the task on CPU 1 to be scheduled */
  88. vTaskDelay(1);
  89. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 4, NULL, 0);
  90. while (true) {
  91. ;
  92. }
  93. }
  94. #endif
  95. void __attribute__((no_sanitize_undefined)) test_storeprohibited(void)
  96. {
  97. *(int*) 0x1 = 0;
  98. }
  99. void IRAM_ATTR test_cache_error(void)
  100. {
  101. spi_flash_disable_interrupts_caches_and_other_cpu();
  102. die("this should not be printed");
  103. }
  104. void IRAM_ATTR test_int_wdt_cache_disabled(void)
  105. {
  106. spi_flash_disable_interrupts_caches_and_other_cpu();
  107. portDISABLE_INTERRUPTS();
  108. while (true) {
  109. ;
  110. }
  111. }
  112. void test_assert(void)
  113. {
  114. assert(0);
  115. }
  116. void IRAM_ATTR test_assert_cache_disabled(void)
  117. {
  118. spi_flash_disable_interrupts_caches_and_other_cpu();
  119. assert(0);
  120. }
  121. /**
  122. * This function overwrites the stack beginning from the valid area continuously towards and beyond
  123. * the end of the stack (stack base) of the current task.
  124. * This is to test stack protection measures like a watchpoint at the end of the stack.
  125. *
  126. * @note: This test DOES NOT write beyond the stack limit. It only writes up to exactly the limit itself.
  127. * The FreeRTOS stack protection mechanisms all trigger shortly before the end of the stack.
  128. */
  129. void test_stack_overflow(void)
  130. {
  131. register uint32_t* sp asm("sp");
  132. TaskStatus_t pxTaskStatus;
  133. vTaskGetInfo(NULL, &pxTaskStatus, pdFALSE, pdFALSE);
  134. uint32_t *end = (uint32_t*) pxTaskStatus.pxStackBase;
  135. // offset - 20 bytes from SP in order to not corrupt the current frame.
  136. // Need to write from higher to lower addresses since the stack grows downwards and the watchpoint/canary is near
  137. // the end of the stack (lowest address).
  138. for (uint32_t* ptr = sp - 5; ptr != end; --ptr) {
  139. *ptr = 0;
  140. }
  141. // trigger a context switch to initiate checking the FreeRTOS stack canary
  142. vTaskDelay(pdMS_TO_TICKS(0));
  143. }
  144. void test_illegal_instruction(void)
  145. {
  146. #if __XTENSA__
  147. __asm__ __volatile__("ill");
  148. #elif __riscv
  149. __asm__ __volatile__("unimp");
  150. #endif
  151. }
  152. void test_instr_fetch_prohibited(void)
  153. {
  154. typedef void (*fptr_t)(void);
  155. volatile fptr_t fptr = (fptr_t) 0x4;
  156. fptr();
  157. }
  158. void test_ub(void)
  159. {
  160. uint8_t stuff[1] = {rand()};
  161. printf("%d\n", stuff[rand()]);
  162. }