On 12/12/2017 02:41 PM, Eric Blake wrote: > On 12/11/2017 03:32 PM, Paolo Bonzini wrote: >> On 11/12/2017 15:11, Eric Blake wrote: >>> I don't know if there is a way to make gcc insert stack-unwind >>> directives that are honored across longjmp (I know C++ does it for >>> exceptions; so there may be a way, and I just don't know it). >> >> Probably -fexceptions. >> > > Well, that's what 'info gcc' mentions: > > 'cleanup (CLEANUP_FUNCTION)' > The 'cleanup' attribute runs a function when the variable goes out > of scope. This attribute can only be applied to auto function > scope variables; it may not be applied to parameters or variables > with static storage duration. The function must take one > parameter, a pointer to a type compatible with the variable. The > return value of the function (if any) is ignored. > > If '-fexceptions' is enabled, then CLEANUP_FUNCTION is run during > the stack unwinding that happens during the processing of the > exception. Note that the 'cleanup' attribute does not allow the > exception to be caught, only to perform an action. It is undefined > what happens if CLEANUP_FUNCTION does not return normally. > > but adding -fexceptions to my sample program does NOT make a difference > (apparently, unwind cleanup triggered by C++ exceptions is NOT the same > as unwinding done by longjmp()).
longjmp isn't an exception, and so doesn't run stack cleanups. You'd need to use _Unwind_Throw ... except for the fact that C doesn't have a mechanism by which we can catch it. We would really need to use C++ and throw to get those semantics. r~