test_core_dump.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Application For Core Dumps Generation
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_system.h"
  11. #include "nvs_flash.h"
  12. #include "unity.h"
  13. // task crash indicators
  14. #define TCI_NULL_PTR 0x1
  15. #define TCI_UNALIGN_PTR 0x2
  16. #define TCI_FAIL_ASSERT 0x4
  17. volatile unsigned long crash_flags = TCI_UNALIGN_PTR;
  18. void bad_ptr_func(void)
  19. {
  20. unsigned long *ptr = (unsigned long *)0;
  21. volatile int cnt = 0;
  22. int i = 0;
  23. for (i = 0; i < 1000; i++) {
  24. cnt++;
  25. }
  26. if(crash_flags & TCI_NULL_PTR) {
  27. printf("Write to bad address 0x%lx.\n", (unsigned long)ptr);
  28. *ptr = 0xDEADBEEF;
  29. }
  30. }
  31. void bad_ptr_task(void *pvParameter)
  32. {
  33. printf("Task 'bad_ptr_task' start.\n");
  34. while (1) {
  35. vTaskDelay(1000 / portTICK_RATE_MS);
  36. printf("Task 'bad_ptr_task' run.\n");
  37. bad_ptr_func();
  38. }
  39. fflush(stdout);
  40. }
  41. void recur_func(void)
  42. {
  43. static int rec_cnt;
  44. unsigned short *ptr = (unsigned short *)0x5;
  45. volatile int cnt = 0;
  46. int i = 0;
  47. if (rec_cnt++ > 2) {
  48. return;
  49. }
  50. for (i = 0; i < 4; i++) {
  51. cnt++;
  52. if(i == 2) {
  53. recur_func();
  54. break;
  55. }
  56. }
  57. if(crash_flags & TCI_UNALIGN_PTR) {
  58. printf("Write to unaligned address 0x%lx.\n", (unsigned long)ptr);
  59. *ptr = 0xDEAD;
  60. }
  61. }
  62. void unaligned_ptr_task(void *pvParameter)
  63. {
  64. printf("Task 'unaligned_ptr_task' start.\n");
  65. while (1) {
  66. vTaskDelay(1000 / portTICK_RATE_MS);
  67. printf("Task 'unaligned_ptr_task' run.\n");
  68. recur_func();
  69. }
  70. fflush(stdout);
  71. }
  72. void failed_assert_task(void *pvParameter)
  73. {
  74. printf("Task 'failed_assert_task' start.\n");
  75. while (1) {
  76. vTaskDelay(1000 / portTICK_RATE_MS);
  77. printf("Task 'failed_assert_task' run.\n");
  78. if(crash_flags & TCI_FAIL_ASSERT) {
  79. printf("Assert.\n");
  80. assert(0);
  81. }
  82. }
  83. fflush(stdout);
  84. }
  85. TEST_CASE("verify coredump functionality", "[coredump][ignore]")
  86. {
  87. nvs_flash_init();
  88. xTaskCreate(&bad_ptr_task, "bad_ptr_task", 2048, NULL, 5, NULL);
  89. xTaskCreatePinnedToCore(&unaligned_ptr_task, "unaligned_ptr_task", 2048, NULL, 7, NULL, 1);
  90. xTaskCreatePinnedToCore(&failed_assert_task, "failed_assert_task", 2048, NULL, 10, NULL, 0);
  91. }