test_panic_main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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();
  13. /* functions which cause an exception/panic in different ways */
  14. static void test_abort();
  15. static void test_int_wdt();
  16. static void test_task_wdt();
  17. static void test_storeprohibited();
  18. static void test_cache_error();
  19. static void test_int_wdt_cache_disabled();
  20. static void test_stack_overflow();
  21. static void test_illegal_instruction();
  22. static void test_instr_fetch_prohibited();
  23. void app_main(void)
  24. {
  25. /* Needed to allow the tick hook to set correct INT WDT timeouts */
  26. vTaskDelay(2);
  27. /* Test script sends to command over UART. Read it and determine how to proceed. */
  28. const char* test_name = get_test_name();
  29. if (test_name == NULL) {
  30. /* Nothing to do */
  31. return;
  32. }
  33. printf("Got test name: %s\n", test_name);
  34. #define HANDLE_TEST(name_) \
  35. if (strcmp(test_name, #name_) == 0) { \
  36. name_(); \
  37. die("Test function has returned"); \
  38. }
  39. HANDLE_TEST(test_abort);
  40. HANDLE_TEST(test_int_wdt);
  41. HANDLE_TEST(test_task_wdt);
  42. HANDLE_TEST(test_storeprohibited);
  43. HANDLE_TEST(test_cache_error);
  44. HANDLE_TEST(test_int_wdt_cache_disabled);
  45. HANDLE_TEST(test_stack_overflow);
  46. HANDLE_TEST(test_illegal_instruction);
  47. HANDLE_TEST(test_instr_fetch_prohibited);
  48. #undef HANDLE_TEST
  49. die("Unknown test name");
  50. }
  51. /* implementations of the test functions */
  52. static void test_abort()
  53. {
  54. abort();
  55. }
  56. static void test_int_wdt()
  57. {
  58. portDISABLE_INTERRUPTS();
  59. while (true) {
  60. ;
  61. }
  62. }
  63. static void test_task_wdt()
  64. {
  65. while (true) {
  66. ;
  67. }
  68. }
  69. static void test_storeprohibited()
  70. {
  71. *(int*) 0x1 = 0;
  72. }
  73. static IRAM_ATTR void test_cache_error()
  74. {
  75. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  76. die("this should not be printed");
  77. }
  78. static void IRAM_ATTR test_int_wdt_cache_disabled()
  79. {
  80. esp_flash_default_chip->os_func->start(esp_flash_default_chip->os_func_data);
  81. portDISABLE_INTERRUPTS();
  82. while (true) {
  83. ;
  84. }
  85. }
  86. static void test_stack_overflow()
  87. {
  88. volatile uint8_t stuff[CONFIG_ESP_MAIN_TASK_STACK_SIZE * 2];
  89. for (int i = 0; i < sizeof(stuff); ++i) {
  90. stuff[i] = rand();
  91. }
  92. }
  93. static void test_illegal_instruction()
  94. {
  95. __asm__ __volatile__("ill");
  96. }
  97. static void test_instr_fetch_prohibited()
  98. {
  99. typedef void (*fptr_t)(void);
  100. volatile fptr_t fptr = (fptr_t) 0x4;
  101. fptr();
  102. }
  103. /* implementations of the utility functions */
  104. #define BOOT_CMD_MAX_LEN (128)
  105. static const char* get_test_name()
  106. {
  107. static char test_name_str[BOOT_CMD_MAX_LEN] = {0};
  108. printf("Enter test name: ");
  109. fflush(stdout);
  110. /* Not using blocking fgets(stdin) here, as QEMU doesn't yet implement RX timeout interrupt,
  111. * which is required for the UART driver and blocking stdio to work.
  112. */
  113. int c = EOF;
  114. char *p = test_name_str;
  115. const char *end = test_name_str + sizeof(test_name_str) - 1;
  116. while (p < end) {
  117. c = getchar();
  118. if (c == EOF) {
  119. vTaskDelay(pdMS_TO_TICKS(10));
  120. } else if (c == '\r') {
  121. continue;
  122. } else if (c == '\n') {
  123. *p = '\0';
  124. break;
  125. } else {
  126. *p = c;
  127. ++p;
  128. }
  129. }
  130. return test_name_str;
  131. }
  132. extern void esp_restart_noos(void) __attribute__ ((noreturn));
  133. static void die(const char* msg)
  134. {
  135. printf("Test error: %s\n\n", msg);
  136. fflush(stdout);
  137. fsync(fileno(stdout));
  138. /* Don't use abort here as it would enter the panic handler */
  139. esp_restart_noos();
  140. }