test_panic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "hal/mpu_hal.h"
  19. /* Test utility function */
  20. extern void esp_restart_noos(void) __attribute__ ((noreturn));
  21. void die(const char* msg)
  22. {
  23. printf("Test error: %s\n\n", msg);
  24. fflush(stdout);
  25. fsync(fileno(stdout));
  26. usleep(1000);
  27. /* Don't use abort here as it would enter the panic handler */
  28. esp_restart_noos();
  29. }
  30. /* implementations of the test functions */
  31. void test_abort(void)
  32. {
  33. abort();
  34. }
  35. void IRAM_ATTR test_abort_cache_disabled(void)
  36. {
  37. spi_flash_disable_interrupts_caches_and_other_cpu();
  38. abort();
  39. }
  40. void test_int_wdt(void)
  41. {
  42. portDISABLE_INTERRUPTS();
  43. while (true) {
  44. ;
  45. }
  46. }
  47. void test_task_wdt_cpu0(void)
  48. {
  49. while (true) {
  50. ;
  51. }
  52. }
  53. __attribute__((optimize("-O0")))
  54. void test_hw_stack_guard_cpu0(void)
  55. {
  56. uint32_t buf[128];
  57. test_hw_stack_guard_cpu0();
  58. }
  59. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  60. static void stack_in_extram(void* arg) {
  61. (void) arg;
  62. /* Abort instead of using a load/store prohibited to prevent a sanitize error */
  63. abort();
  64. }
  65. void test_panic_extram_stack(void) {
  66. /* Start by initializing a Task which has a stack in external RAM */
  67. StaticTask_t handle;
  68. const uint32_t stack_size = 8192;
  69. void* stack = heap_caps_malloc(stack_size, MALLOC_CAP_SPIRAM);
  70. /* Make sure the stack is in external RAM */
  71. if (!esp_ptr_external_ram(stack)) {
  72. die("Allocated stack is not in external RAM!\n");
  73. }
  74. xTaskCreateStatic(stack_in_extram, "Task_stack_extram", stack_size, NULL, 4, (StackType_t*) stack, &handle);
  75. vTaskDelay(1000);
  76. }
  77. #endif // ESP_COREDUMP_ENABLE_TO_FLASH && SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  78. #if !CONFIG_FREERTOS_UNICORE
  79. static void infinite_loop(void* arg) {
  80. (void) arg;
  81. while(1) {
  82. ;
  83. }
  84. }
  85. void test_task_wdt_cpu1(void)
  86. {
  87. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 1, NULL, 1);
  88. while (true) {
  89. vTaskDelay(1);
  90. }
  91. }
  92. void test_task_wdt_both_cpus(void)
  93. {
  94. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 4, NULL, 1);
  95. /* Give some time to the task on CPU 1 to be scheduled */
  96. vTaskDelay(1);
  97. xTaskCreatePinnedToCore(infinite_loop, "Infinite loop", 1024, NULL, 4, NULL, 0);
  98. while (true) {
  99. ;
  100. }
  101. }
  102. #endif
  103. void __attribute__((no_sanitize_undefined)) test_storeprohibited(void)
  104. {
  105. *(int*) 0x1 = 0;
  106. }
  107. void IRAM_ATTR test_cache_error(void)
  108. {
  109. spi_flash_disable_interrupts_caches_and_other_cpu();
  110. die("this should not be printed");
  111. }
  112. void IRAM_ATTR test_int_wdt_cache_disabled(void)
  113. {
  114. spi_flash_disable_interrupts_caches_and_other_cpu();
  115. portDISABLE_INTERRUPTS();
  116. while (true) {
  117. ;
  118. }
  119. }
  120. void test_assert(void)
  121. {
  122. assert(0);
  123. }
  124. void IRAM_ATTR test_assert_cache_disabled(void)
  125. {
  126. spi_flash_disable_interrupts_caches_and_other_cpu();
  127. assert(0);
  128. }
  129. /**
  130. * This function overwrites the stack beginning from the valid area continuously towards and beyond
  131. * the end of the stack (stack base) of the current task.
  132. * This is to test stack protection measures like a watchpoint at the end of the stack.
  133. *
  134. * @note: This test DOES NOT write beyond the stack limit. It only writes up to exactly the limit itself.
  135. * The FreeRTOS stack protection mechanisms all trigger shortly before the end of the stack.
  136. */
  137. void test_stack_overflow(void)
  138. {
  139. register uint32_t* sp asm("sp");
  140. TaskStatus_t pxTaskStatus;
  141. vTaskGetInfo(NULL, &pxTaskStatus, pdFALSE, pdFALSE);
  142. uint32_t *end = (uint32_t*) pxTaskStatus.pxStackBase;
  143. // offset - 20 bytes from SP in order to not corrupt the current frame.
  144. // Need to write from higher to lower addresses since the stack grows downwards and the watchpoint/canary is near
  145. // the end of the stack (lowest address).
  146. for (uint32_t* ptr = sp - 5; ptr != end; --ptr) {
  147. *ptr = 0;
  148. }
  149. // trigger a context switch to initiate checking the FreeRTOS stack canary
  150. vTaskDelay(pdMS_TO_TICKS(0));
  151. }
  152. void test_illegal_instruction(void)
  153. {
  154. #if __XTENSA__
  155. __asm__ __volatile__("ill");
  156. #elif __riscv
  157. __asm__ __volatile__("unimp");
  158. #endif
  159. }
  160. void test_instr_fetch_prohibited(void)
  161. {
  162. typedef void (*fptr_t)(void);
  163. volatile fptr_t fptr = (fptr_t) 0x4;
  164. fptr();
  165. }
  166. void test_ub(void)
  167. {
  168. uint8_t stuff[1] = {rand()};
  169. printf("%d\n", stuff[rand()]);
  170. }
  171. /* NOTE: The following test verifies the behaviour for the
  172. * Xtensa-specific MPU instructions (Refer WDTLB, DSYNC, WDTIB, ISYNC)
  173. * used for memory protection.
  174. *
  175. * However, this test is not valid for S2 and S3, because they have PMS
  176. * enabled on top of this, giving unpredicatable results.
  177. */
  178. #if CONFIG_IDF_TARGET_ESP32
  179. void test_illegal_access(void)
  180. {
  181. intptr_t addr = 0x80000000; // MPU region 4
  182. volatile int __attribute__((unused)) val = INT16_MAX;
  183. // Marked as an illegal access region at startup in ESP32, ESP32S2.
  184. // Make accessible temporarily.
  185. mpu_hal_set_region_access(4, MPU_REGION_RW);
  186. val = *((int*) addr);
  187. printf("[1] val: %d at %p\n", val, (void *)addr);
  188. // Make access to region illegal again.
  189. mpu_hal_set_region_access(4, MPU_REGION_ILLEGAL);
  190. val = *((int*) addr);
  191. // Does not reach here as device resets due to illegal access
  192. printf("[2] val: %d at %p\n", val, (void *)addr);
  193. }
  194. #endif