test_panic.c 4.7 KB

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