News for hackers. Stuff that matters.
Re:Let's write a setjmp by adrian_b on Sunday February 12, 2023 @05:07AM (#34761601)
"setjmp/longjmp" must save/restore only those registers that are defined by the C ABI as being preserved across function calls.

The program point to where "longjmp" jumps is viewed by the compiler as a function return point, so the compiler assumes that all the other registers hold undefined values.

For most CPUs, the C ABI defines the SIMD registers as being not preserved across function calls, so "setjmp" does not need to save them. Had some of them been defined as preserved registers, "setjmp" would have also saved those.

"setjmp/longjmp" cannot be used in any C++ program, unless all the nested functions that occur between a "setjmp" and a "longjmp" are C functions. This may happen in C libraries which are linked into C++ programs and which may use "setjmp/longjmp" internally, without interfering with C++ objects.

As you say, "attribute(cleanup)" is not something specified in the C language standard, so you cannot expect that "longjmp" will invoke the cleanup function, unless you use a specific libc implementation, whose documentation explicitly says that its "longjmp" will invoke the cleanup functions, when the program is compiled with a certain C compiler. I am not aware of any such "longjmp" implementation.

If you want to use exceptions in a C program, you must use "setjmp/longjmp". If you do not want to use exceptions in a C program, you have no need for them.

The same happens in any other programming language. If you do not want to use exceptions in a C++ program, you have no need to use keywords like "try" and "throw".

Re:Let's write a setjmp by murdoze on Sunday February 12, 2023 @06:01AM (#34761892)
One of the largest C++ projects I worked on was compiled with exceptions disabled (-fno-exceptions).

Still in production, for 20 years now.

Re:Let's write a setjmp by chaosite on Sunday February 12, 2023 @06:28AM (#34762018)
Google famously doesn't use exceptions for C++, and Google's C++ code is not exception-safe (https://google.github.io/styleguide/cppguide.html#Exceptions.)
Re:Let's write a setjmp by flohofwoe on Sunday February 12, 2023 @06:05AM (#34761906)
I wonder though if setjmp/longjmp was designed with exceptions in mind, or just as a general escape hatch for the 'tamed' structured goto in C (my guess would be the latter).
Re:Let's write a setjmp by bregma on Sunday February 12, 2023 @07:05AM (#34762206)
Specifically, The C++ Programming Language standard ISO/IEC 14882:2011 18.10/4 [support.runtime] says this.

> A setjmp/longjmp call pair has undefined behavior if replacing the setjmp and longjmp by catch and throw would invoke any non-trivial destructors for any automatic objects.

Of course, the C++ language runtime is usually written in pure C, and at least one naive implementation of the C++ try/catch mechanism uses setjmp/longjmp under the hood.

Re:Let's write a setjmp by comex on Sunday February 12, 2023 @02:33PM (#34766044)
> I am not aware of any such "longjmp" implementation.

Clang on Windows behaves this way! That is, when targeting the Visual C++ runtime, as opposed to MinGW.

With the Visual C++ toolchain and runtime, longjmp behaves like a C++ exception throw and goes through a whole stack unwinding routine, unlike on Unix (and MinGW) where it just sets a few registers and branches. This has the downside of being much slower, but the benefit that longjmp will run destructors of C++ local variables as it unwinds (as well as SEH `__finally` blocks), making it less of a footgun in C++ code.

Visual C++ itself doesn't support GCC extensions like `__attribute__((cleanup))`, but these days Clang is highly compatible with Visual C++ while also supporting GCC extensions.