test_panic_main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_partition.h"
  8. #include "esp_flash.h"
  9. #include "esp_system.h"
  10. /* utility functions */
  11. static void die(const char* msg) __attribute__ ((noreturn));
  12. static const char* get_test_name(void);
  13. /* functions which cause an exception/panic in different ways */
  14. static void test_abort(void);
  15. static void test_abort_cache_disabled(void);
  16. static void test_int_wdt(void);
  17. static void test_task_wdt(void);
  18. static void test_storeprohibited(void);
  19. static void test_cache_error(void);
  20. static void test_int_wdt_cache_disabled(void);
  21. static void test_stack_overflow(void);
  22. static void test_illegal_instruction(void);
  23. static void test_instr_fetch_prohibited(void);
  24. static void test_assert(void);
  25. static void test_assert_cache_disabled(void);
  26. void app_main(void)
  27. {
  28. /* Needed to allow the tick hook to set correct INT WDT timeouts */
  29. vTaskDelay(2);
  30. /* Test script sends to command over UART. Read it and determine how to proceed. */
  31. const char* test_name = get_test_name();
  32. if (test_name == NULL) {
  33. /* Nothing to do */
  34. return;
  35. }
  36. printf("Got test name: %s\n", test_name);
  37. #define HANDLE_TEST(name_) \
  38. if (strcmp(test_name, #name_) == 0) { \
  39. name_(); \
  40. die("Test function has returned"); \
  41. }
  42. HANDLE_TEST(test_abort);
  43. HANDLE_TEST(test_abort_cache_disabled);
  44. HANDLE_TEST(test_int_wdt);
  45. HANDLE_TEST(test_task_wdt);
  46. HANDLE_TEST(test_storeprohibited);
  47. HANDLE_TEST(test_cache_error);
  48. HANDLE_TEST(test_int_wdt_cache_disabled);
  49. HANDLE_TEST(test_stack_overflow);
  50. HANDLE_TEST(test_illegal_instruction);
  51. HANDLE_TEST(test_instr_fetch_prohibited);
  52. HANDLE_TEST(test_assert);
  53. HANDLE_TEST(test_assert_cache_disabled);
  54. #undef HANDLE_TEST
  55. die("Unknown test name");
  56. }
  57. /* implementations of the test functions */
  58. static void test_abort(void)
  59. {
  60. abort();
  61. }
  62. static void IRAM_ATTR test_abort_cache_disabled(void)
  63. {
  64. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  65. abort();
  66. }
  67. static void test_int_wdt(void)
  68. {
  69. portDISABLE_INTERRUPTS();
  70. while (true) {
  71. ;
  72. }
  73. }
  74. static void test_task_wdt(void)
  75. {
  76. while (true) {
  77. ;
  78. }
  79. }
  80. static void test_storeprohibited(void)
  81. {
  82. *(int*) 0x1 = 0;
  83. }
  84. static IRAM_ATTR void test_cache_error(void)
  85. {
  86. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  87. die("this should not be printed");
  88. }
  89. static void IRAM_ATTR test_int_wdt_cache_disabled(void)
  90. {
  91. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  92. portDISABLE_INTERRUPTS();
  93. while (true) {
  94. ;
  95. }
  96. }
  97. static void test_assert(void)
  98. {
  99. assert(0);
  100. }
  101. static void IRAM_ATTR test_assert_cache_disabled(void)
  102. {
  103. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  104. assert(0);
  105. }
  106. /**
  107. * This function overwrites the stack beginning from the valid area continuously towards and beyond
  108. * the end of the stack (stack base) of the current task.
  109. * This is to test stack protection measures like a watchpoint at the end of the stack.
  110. *
  111. * @note: This test DOES NOT write beyond the stack limit. It only writes up to exactly the limit itself.
  112. * The FreeRTOS stack protection mechanisms all trigger shortly before the end of the stack.
  113. */
  114. static void test_stack_overflow(void)
  115. {
  116. register uint32_t* sp asm("sp");
  117. TaskStatus_t pxTaskStatus;
  118. vTaskGetInfo(NULL, &pxTaskStatus, pdFALSE, pdFALSE);
  119. uint32_t *end = (uint32_t*) pxTaskStatus.pxStackBase;
  120. // offset - 20 bytes from SP in order to not corrupt the current frame.
  121. // Need to write from higher to lower addresses since the stack grows downwards and the watchpoint/canary is near
  122. // the end of the stack (lowest address).
  123. for (uint32_t* ptr = sp - 5; ptr != end; --ptr) {
  124. *ptr = 0;
  125. }
  126. // trigger a context switch to initiate checking the FreeRTOS stack canary
  127. vTaskDelay(pdMS_TO_TICKS(0));
  128. }
  129. static void test_illegal_instruction(void)
  130. {
  131. #if __XTENSA__
  132. __asm__ __volatile__("ill");
  133. #elif __riscv
  134. __asm__ __volatile__("unimp");
  135. #endif
  136. }
  137. static void test_instr_fetch_prohibited(void)
  138. {
  139. typedef void (*fptr_t)(void);
  140. volatile fptr_t fptr = (fptr_t) 0x4;
  141. fptr();
  142. }
  143. /* implementations of the utility functions */
  144. #define BOOT_CMD_MAX_LEN (128)
  145. static const char* get_test_name(void)
  146. {
  147. static char test_name_str[BOOT_CMD_MAX_LEN] = {0};
  148. printf("Enter test name: ");
  149. fflush(stdout);
  150. /* Not using blocking fgets(stdin) here, as QEMU doesn't yet implement RX timeout interrupt,
  151. * which is required for the UART driver and blocking stdio to work.
  152. */
  153. int c = EOF;
  154. char *p = test_name_str;
  155. const char *end = test_name_str + sizeof(test_name_str) - 1;
  156. while (p < end) {
  157. c = getchar();
  158. if (c == EOF) {
  159. vTaskDelay(pdMS_TO_TICKS(10));
  160. } else if (c == '\r') {
  161. continue;
  162. } else if (c == '\n') {
  163. *p = '\0';
  164. break;
  165. } else {
  166. *p = c;
  167. ++p;
  168. }
  169. }
  170. return test_name_str;
  171. }
  172. extern void esp_restart_noos(void) __attribute__ ((noreturn));
  173. static void die(const char* msg)
  174. {
  175. printf("Test error: %s\n\n", msg);
  176. fflush(stdout);
  177. fsync(fileno(stdout));
  178. /* Don't use abort here as it would enter the panic handler */
  179. esp_restart_noos();
  180. }