toolchain: C++ exception workaround

* disable C++ exception FDE sorting (saves RAM)

Closes IDF-1128
This commit is contained in:
Jakob Hasse 2020-04-24 15:41:08 +08:00 committed by Anton Maklakov
parent 1797fa7716
commit 8329d51fd0
2 changed files with 155 additions and 14 deletions

View File

@ -44,31 +44,161 @@ TEST_CASE("can use std::vector", "[cxx]")
TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0)); TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
} }
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~300 bytes):
- 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
This info is kept until global destructors are called by __do_global_dtors_aux()
- 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
- 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
- 88 bytes are allocated by pthread_setspecific() to init internal lock
- some more memory...
*/
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
TEST_CASE("c++ exceptions work", "[cxx]") TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=300]")
{ {
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
- 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
This info is kept until global destructors are called by __do_global_dtors_aux()
- 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
- 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
- 88 bytes are allocated by pthread_setspecific() to init internal lock
*/
int thrown_value; int thrown_value;
try try {
{
throw 20; throw 20;
} } catch (int e) {
catch (int e)
{
thrown_value = e; thrown_value = e;
} }
TEST_ASSERT_EQUAL(20, thrown_value); TEST_ASSERT_EQUAL(20, thrown_value);
printf("OK?\n"); printf("OK?\n");
} }
TEST_CASE("c++ exceptions emergency pool", "[cxx] [ignore]") TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=300]")
{
bool thrown_value = false;
try {
throw true;
} catch (bool e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(true, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=300]")
{
void* thrown_value = 0;
try {
throw (void*) 47;
} catch (void* e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=300]")
{
uint64_t thrown_value = 0;
try {
throw (uint64_t) 47;
} catch (uint64_t e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=300]")
{
char thrown_value = '0';
try {
throw '/';
} catch (char e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL('/', thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=300]")
{
wchar_t thrown_value = 0;
try {
throw (wchar_t) 47;
} catch (wchar_t e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=300]")
{
float thrown_value = 0;
try {
throw 23.5f;
} catch (float e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(23.5, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=300]")
{
double thrown_value = 0;
try {
throw 23.5d;
} catch (double e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(23.5d, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=300]")
{
const char *thrown_value = 0;
try {
throw "Hi :)";
} catch (const char *e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
printf("OK?\n");
}
struct NonExcTypeThrowee {
int value;
public:
NonExcTypeThrowee(int value) : value(value) { }
};
TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=300]")
{
int thrown_value = 0;
try {
throw NonExcTypeThrowee(47);
} catch (NonExcTypeThrowee &e) {
thrown_value = e.value;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
struct ExcTypeThrowee : public std::exception {
int value;
public:
ExcTypeThrowee(int value) : value(value) { }
};
TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=300]")
{
int thrown_value = 0;
try {
throw ExcTypeThrowee(47);
} catch (ExcTypeThrowee &e) {
thrown_value = e.value;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore] [leaks=300]")
{ {
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes): /* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
- 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions. - 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.

View File

@ -108,6 +108,11 @@ struct object { long placeholder[ 10 ]; };
void __register_frame_info (const void *begin, struct object *ob); void __register_frame_info (const void *begin, struct object *ob);
extern char __eh_frame[]; extern char __eh_frame[];
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
// workaround for C++ exception large memory allocation
void _Unwind_SetEnableExceptionFdeSorting(unsigned char enable);
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
//If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false. //If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
static bool s_spiram_okay=true; static bool s_spiram_okay=true;
@ -382,6 +387,12 @@ void start_cpu0_default(void)
assert(err == ESP_OK && "Failed to init pthread module!"); assert(err == ESP_OK && "Failed to init pthread module!");
do_global_ctors(); do_global_ctors();
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
ESP_EARLY_LOGD(TAG, "Setting C++ exception workarounds.");
_Unwind_SetEnableExceptionFdeSorting(0);
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
#if CONFIG_ESP_INT_WDT #if CONFIG_ESP_INT_WDT
esp_int_wdt_init(); esp_int_wdt_init();
//Initialize the interrupt watch dog for CPU0. //Initialize the interrupt watch dog for CPU0.