test_panic_main.c 5.0 KB

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