1 // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdio.h>
16 #include "string.h"
17 #include "esp_heap_caps.h"
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/task.h"
20 #include "unity.h"
21 #include "unity_test_runner.h"
22 #include "test_utils.h"
23 #include "esp_newlib.h"
24 
25 #ifdef CONFIG_HEAP_TRACING
26 #include "esp_heap_trace.h"
27 #endif
28 
29 static size_t before_free_8bit;
30 static size_t before_free_32bit;
31 
32 static size_t warn_leak_threshold;
33 static size_t critical_leak_threshold;
34 
unity_task(void * pvParameters)35 static void unity_task(void *pvParameters)
36 {
37     vTaskDelay(2); /* Delay a bit to let the main task be deleted */
38     unity_run_menu(); /* Doesn't return */
39 }
40 
test_main(void)41 void test_main(void)
42 {
43     // Note: if unpinning this task, change the way run times are calculated in
44     // unity_port_esp32.c
45     xTaskCreatePinnedToCore(unity_task, "unityTask", UNITY_FREERTOS_STACK_SIZE, NULL,
46                             UNITY_FREERTOS_PRIORITY, NULL, UNITY_FREERTOS_CPU);
47 }
48 
unity_reset_leak_checks(void)49 void unity_reset_leak_checks(void)
50 {
51     before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
52     before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
53 
54 #ifdef CONFIG_HEAP_TRACING
55     heap_trace_start(HEAP_TRACE_LEAKS);
56 #endif
57 }
58 
59 /* setUp runs before every test */
setUp(void)60 void setUp(void)
61 {
62 // If heap tracing is enabled in kconfig, leak trace the test
63 #ifdef CONFIG_HEAP_TRACING
64     const size_t num_heap_records = 80;
65     static heap_trace_record_t *record_buffer;
66     if (!record_buffer) {
67         record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
68         assert(record_buffer);
69         heap_trace_init_standalone(record_buffer, num_heap_records);
70     }
71 #endif
72 
73     printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
74 
75 #ifdef CONFIG_APP_BUILD_USE_FLASH_SECTIONS
76     /* TODO: add sufficient startup code in case of building an ELF file, so that
77      * flash cache is initialized and can work in such mode.
78      * For now this is disabled to allow running unit tests which don't require
79      * flash cache related operations.
80      */
81     get_test_data_partition();  /* allocate persistent partition table structures */
82 #endif // CONFIG_APP_BUILD_USE_FLASH_SECTIONS
83 
84     unity_reset_leak_checks();
85     test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, TYPE_LEAK_CRITICAL, COMP_LEAK_GENERAL);
86     test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, TYPE_LEAK_WARNING, COMP_LEAK_GENERAL);
87 }
88 
check_leak(size_t before_free,size_t after_free,const char * type)89 static void check_leak(size_t before_free, size_t after_free, const char *type)
90 {
91     int free_delta = (int)after_free - (int)before_free;
92     printf("MALLOC_CAP_%s usage: Free memory delta: %d Leak threshold: -%u \n",
93            type,
94            free_delta,
95            critical_leak_threshold);
96 
97     if (free_delta > 0) {
98         return; // free memory went up somehow
99     }
100 
101     size_t leaked = (size_t)(free_delta * -1);
102     if (leaked <= warn_leak_threshold) {
103         return;
104     }
105 
106     printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
107            type,
108            leaked <= critical_leak_threshold ? "potential" : "critical",
109            before_free, after_free, leaked);
110     fflush(stdout);
111     TEST_ASSERT_MESSAGE(leaked <= critical_leak_threshold, "The test leaked too much memory");
112 }
113 
leak_check_required(void)114 static bool leak_check_required(void)
115 {
116     warn_leak_threshold = test_utils_get_leak_level(TYPE_LEAK_WARNING, COMP_LEAK_ALL);
117     critical_leak_threshold = test_utils_get_leak_level(TYPE_LEAK_CRITICAL, COMP_LEAK_ALL);
118     if (Unity.CurrentDetail1 != NULL) {
119         const char *leaks = "[leaks";
120         const int len_leaks = strlen(leaks);
121         const char *sub_leaks = strstr(Unity.CurrentDetail1, leaks);
122         if (sub_leaks != NULL) {
123             if (sub_leaks[len_leaks] == ']') {
124                 return false;
125             } else if (sub_leaks[len_leaks] == '=') {
126                 critical_leak_threshold = strtol(&sub_leaks[len_leaks + 1], NULL, 10);
127                 warn_leak_threshold = critical_leak_threshold;
128                 return true;
129             }
130         }
131     }
132     return true;
133 }
134 
135 /* tearDown runs after every test */
tearDown(void)136 void tearDown(void)
137 {
138     /* some FreeRTOS stuff is cleaned up by idle task */
139     vTaskDelay(5);
140 
141     /* clean up some of the newlib's lazy allocations */
142     esp_reent_cleanup();
143 
144     /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
145     const char *real_testfile = Unity.TestFile;
146     Unity.TestFile = __FILE__;
147 
148     /* check if unit test has caused heap corruption in any heap */
149     TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
150 
151     /* check for leaks */
152 #ifdef CONFIG_HEAP_TRACING
153     heap_trace_stop();
154     heap_trace_dump();
155 #endif
156 
157     if (leak_check_required()) {
158           size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
159           size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
160           check_leak(before_free_8bit, after_free_8bit, "8BIT");
161           check_leak(before_free_32bit, after_free_32bit, "32BIT");
162     }
163 
164     Unity.TestFile = real_testfile; // go back to the real filename
165 }
166