Hand-written in assembly in libc, setjmp+longjmp

In general avoiding asm is a wonderful idea.

However, there is a limited set of functions which do weird stuff with stack frames, vfork, setjmp, sigsetjmp, longjmp, and siglongjmp, and the internal __restore_rt. For those few functions, I really think you’re much better off using a 100%-asm implementation.

These are so magic, that basically any instrumentation that the compiler might add, like sanitizers, profiling, etc, will break them. And depending on particular compiler options and optimization levels for correctness really seems like a bad idea, especially given the goal of being instrumentation-friendly.

So, then, regarding the two other options: the “naked” attribute on a function with inline-asm, versus an assembly source file. While using the “naked” attribute is theoretically a reasonable answer, I would very much recommend that you NOT use it. Almost the only thing that the ‘naked’ attr really gets you is that you don’t need to write the couple asm lines .globl and .type. However, the attribute creates a weird hybrid kind of function, which makes it quite fragile, and rife with implementation issues, in all compilers. Unfortunately, using it is asking for problems, and I don’t see any particularly interesting benefit.

BTW separately but relatedly, I’d note that it’s also unsafe to create any wrapper functions around these, other than longjmp/siglongjmp. That’s something llvm-libc sometimes does to expose them as public names, right?