test_panic_main.c 5.6 KB

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