test_panic.c 4.6 KB

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