Skip to content

Commit ebe62ee

Browse files
committed
Merge branch 'bugfix/exception_crash_workaround' into 'master'
toolchain: C++ exception workarounds Closes IDF-1128, IDF-1301, and IDF-1804 See merge request espressif/esp-idf!8967
2 parents 22d9ff5 + f4c2f68 commit ebe62ee

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

components/cxx/test/test_cxx.cpp

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ TEST_CASE("can use std::vector", "[cxx]")
4444
TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
4545
}
4646

47-
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
48-
- 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
49-
This info is kept until global destructors are called by __do_global_dtors_aux()
47+
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~300 bytes):
5048
- 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
5149
- 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
5250
- 88 bytes are allocated by pthread_setspecific() to init internal lock
51+
- some more memory...
5352
*/
5453
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
5554

56-
TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=816]")
55+
#if CONFIG_IDF_TARGET_ESP32
56+
#define LEAKS "300"
57+
#elif CONFIG_IDF_TARGET_ESP32S2
58+
#define LEAKS "800"
59+
#else
60+
#error "unknown target in CXX tests, can't set leaks threshold"
61+
#endif
62+
63+
TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=" LEAKS "]")
5764
{
5865
int thrown_value;
5966
try {
@@ -65,7 +72,7 @@ TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=816]")
6572
printf("OK?\n");
6673
}
6774

68-
TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=816]")
75+
TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
6976
{
7077
bool thrown_value = false;
7178
try {
@@ -77,7 +84,7 @@ TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=816]")
7784
printf("OK?\n");
7885
}
7986

80-
TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=816]")
87+
TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
8188
{
8289
void* thrown_value = 0;
8390
try {
@@ -89,7 +96,7 @@ TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=816]")
8996
printf("OK?\n");
9097
}
9198

92-
TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=816]")
99+
TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
93100
{
94101
uint64_t thrown_value = 0;
95102
try {
@@ -101,7 +108,7 @@ TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=816]")
101108
printf("OK?\n");
102109
}
103110

104-
TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=816]")
111+
TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
105112
{
106113
char thrown_value = '0';
107114
try {
@@ -113,7 +120,7 @@ TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=816]")
113120
printf("OK?\n");
114121
}
115122

116-
TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=816]")
123+
TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
117124
{
118125
wchar_t thrown_value = 0;
119126
try {
@@ -125,7 +132,7 @@ TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=816]")
125132
printf("OK?\n");
126133
}
127134

128-
TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=816]")
135+
TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
129136
{
130137
float thrown_value = 0;
131138
try {
@@ -137,7 +144,7 @@ TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=816]")
137144
printf("OK?\n");
138145
}
139146

140-
TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=816]")
147+
TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
141148
{
142149
double thrown_value = 0;
143150
try {
@@ -149,7 +156,7 @@ TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=816]")
149156
printf("OK?\n");
150157
}
151158

152-
TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=816]")
159+
TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
153160
{
154161
const char *thrown_value = 0;
155162
try {
@@ -167,7 +174,7 @@ struct NonExcTypeThrowee {
167174
NonExcTypeThrowee(int value) : value(value) { }
168175
};
169176

170-
TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=816]")
177+
TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
171178
{
172179
int thrown_value = 0;
173180
try {
@@ -185,7 +192,7 @@ struct ExcTypeThrowee : public std::exception {
185192
ExcTypeThrowee(int value) : value(value) { }
186193
};
187194

188-
TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=816]")
195+
TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=" LEAKS "]")
189196
{
190197
int thrown_value = 0;
191198
try {
@@ -197,7 +204,7 @@ TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=816]")
197204
printf("OK?\n");
198205
}
199206

200-
TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore]")
207+
TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore] [leaks=" LEAKS "]")
201208
{
202209
void **p, **pprev = NULL;
203210
int thrown_value = 0;
@@ -244,6 +251,60 @@ TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore]")
244251
#endif
245252
}
246253

254+
255+
#define TIMEOUT 19
256+
257+
#define RECURSION 19
258+
259+
static esp_timer_handle_t crash_timer;
260+
261+
static uint32_t result = 0;
262+
263+
uint32_t calc_fac(uint32_t n) {
264+
if (n == 1 || n == 0) {
265+
return 1;
266+
} else {
267+
return n * calc_fac(n - 1);
268+
}
269+
}
270+
271+
static void timer_cb(void *arg) {
272+
result = calc_fac(RECURSION);
273+
}
274+
275+
// TODO: Not a unit test, refactor to integration test/system test, etc.
276+
TEST_CASE("frequent interrupts don't interfere with c++ exceptions", "[cxx] [exceptions] [leaks=" LEAKS "]")
277+
{// if exception workaround is disabled, this is almost guaranteed to fail
278+
const esp_timer_create_args_t timer_args {
279+
timer_cb,
280+
NULL,
281+
ESP_TIMER_TASK,
282+
"crash_timer"
283+
};
284+
285+
TEST_ESP_OK(esp_timer_create(&timer_args, &crash_timer));
286+
TEST_ESP_OK(esp_timer_start_periodic(crash_timer, TIMEOUT));
287+
288+
for (int i = 0; i < 500; i++) {
289+
bool thrown_value = false;
290+
try {
291+
throw true;
292+
} catch (bool e) {
293+
thrown_value = e;
294+
}
295+
296+
if (thrown_value) {
297+
printf("ex thrown %d\n", i);
298+
} else {
299+
printf("ex not thrown\n");
300+
TEST_ASSERT(false);
301+
}
302+
}
303+
304+
TEST_ESP_OK(esp_timer_stop(crash_timer));
305+
TEST_ESP_OK(esp_timer_delete(crash_timer));
306+
}
307+
247308
#else // !CONFIG_COMPILER_CXX_EXCEPTIONS
248309

249310
TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")

components/esp_system/startup.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ static volatile bool s_system_full_inited = false;
102102
sys_startup_fn_t g_startup_fn[1] = { start_cpu0 };
103103
#endif
104104

105+
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
106+
// workaround for C++ exception crashes
107+
void _Unwind_SetNoFunctionContextInstall(unsigned char enable);
108+
// workaround for C++ exception large memory allocation
109+
void _Unwind_SetEnableExceptionFdeSorting(unsigned char enable);
110+
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
111+
105112
static const char* TAG = "cpu_start";
106113

107114
static void IRAM_ATTR do_global_ctors(void)
@@ -116,7 +123,7 @@ static void IRAM_ATTR do_global_ctors(void)
116123

117124
static struct object ob;
118125
__register_frame_info( __eh_frame, &ob );
119-
#endif
126+
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
120127

121128
void (**p)(void);
122129
for (p = &__init_array_end - 1; p >= &__init_array_start; --p) {
@@ -375,4 +382,11 @@ IRAM_ATTR ESP_SYSTEM_INIT_FN(init_components0, BIT(0))
375382
esp_efuse_init(efuse_partition->address, efuse_partition->size);
376383
}
377384
#endif
378-
}
385+
386+
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
387+
ESP_EARLY_LOGD(TAG, "Setting C++ exception workarounds.");
388+
_Unwind_SetNoFunctionContextInstall(1);
389+
_Unwind_SetEnableExceptionFdeSorting(0);
390+
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
391+
}
392+

0 commit comments

Comments
 (0)