https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105880
Bug ID: 105880 Summary: eh_globals_init destructor not setting _M_init to false Product: gcc Version: 12.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: chrisj at rtems dot org Target Milestone: --- RTEMS 6 with GCC 12 (gcc version 12.1.1 20220509 (RTEMS 6, RSB e73a258a3aa4af8735b589a2d770571b2105ac5f, Newlib 64b2081) (GCC)) is calling `std::terminate()` in `__cxa_get_globals()`. The `_M_init` value is `true` after the static destructor has run. RTEMS is tracking the issue with https://devel.rtems.org/ticket/4661. When `exit()` is called the static destructor for `eh_gloabls init` runs and deletes the POSIX key however the `_M_init` variable is not set to false. Later `__cxxabiv1::__cxa_get_globals()` is called and sees `_M_init` is `true` so attempts to get the POSIX key value. This fails because the key has been deleted so `NULL` is returned. Memory is allocated and the set fails because there is no key so `std::terminate()` is called. The issue is `init._M_init` is not set to false in the static destructor. The code for aarch64 and arm is: 006562e0 <__eh_globals_init::~__eh_globals_init()>: 6562e0: 7903 ldrb r3, [r0, #4] 6562e2: b510 push {r4, lr} 6562e4: 4604 mov r4, r0 6562e6: b90b cbnz r3, 6562ec <__eh_globals_init::~__eh_globals_init()+0xc> 6562e8: 4620 mov r0, r4 6562ea: bd10 pop {r4, pc} 6562ec: 6800 ldr r0, [r0, #0] 6562ee: f04d fe6b bl 6a3fc8 <pthread_key_delete> 6562f2: 4620 mov r0, r4 6562f4: bd10 pop {r4, pc} 0000000010222c30 <__eh_globals_init::~__eh_globals_init()>: 10222c30: 39401001 ldrb w1, [x0, #4] 10222c34: 35000041 cbnz w1, 10222c3c <__eh_globals_init::~__eh_globals_init()+0xc> 10222c38: d65f03c0 ret 10222c3c: a9bf7bfd stp x29, x30, [sp, #-16]! 10222c40: 910003fd mov x29, sp return pthread_key_delete (__key); 10222c44: b9400000 ldr w0, [x0] 10222c48: 94008806 bl 10244c60 <pthread_key_delete> 10222c4c: a8c17bfd ldp x29, x30, [sp], #16 10222c50: d65f03c0 ret