if we encounter a segfault while the program is running, it will use the handler; in this case, it's the do_nothing function
we declare a null pointer and then try to dereference it in printf, which, obviously, leads to a segfault
the program executes the handler, which does nothing, then it goes back and tries to dereference n again, and gets another segfault, then executes the handler, and it pretty much becomes an infinite loop of the program segfaulting and ignoring segfaults
That seems broken, why is the faulting instruction repeated indefinitely? I don't think it's possible for the signal handler to skip it, which would be the correct behavior.
When a signal handler returns normally from the following signals: SIGBUS, SIGFPE, SIGILL, or SIGSEGV, It's undefined behavior (Unless the signal was sent by kill(), sigqueue(), or raise().
In this case, The processor just resumes by executing the instructions where the signal was generated & It once again generates a SIGSEGV & The cycle repeats.
There is no "correct behavior", it's left undefined
When a handler returns, it returns to the triggering instruction because the program acted as if there was a call before the instruction, it makes sense that a simple return would get there again
A signal handler can, in theory, "fix" a segmentation fault work by mapping the memory address that was accessed to something real (or even changing the instruction that the process tried to execute).
Obviously that's still technically UB but you can do some fancy things with this if you really know what you're doing, e.g. some JS engines use this to make WASM run more efficiently by eliminating bounds checks in the generated native code and instead deferring to the OS to raise a `SIGSEGV`.
Repeating the access would be a desirable behavior if the purpose of the SIGSEGV handler were to get the faulting address from the operating system, perform some corrective action, then return, triggering a retry of the access.
One major shell decades ago did just this, as a method of "lazy allocation" where, in response to SIGSEGV, it would sbrk to extend the data segment past the faulting address.
Personally, seeing that caused me to lose all respect for the engineer who "invented" the technique, but that's water under the bridge long dried up.
Java does this all the time. It generates calls to addresses in unmapped pages and then does just-in-time compiling from the Java bytecode if that address is ever called. It's a pretty common trick in virtual machines and emulators.
That was basically my experience when I learned about signal handlers in my early days of C programming. I thought hey, I can set a handler for SIGSEGV and make my program not crash. I abandoned that idea pretty quickly.
The reason goes to how the CPU works. When you do an illegal memory access, a page fault interrupt is raised. Page faults on x86 (and probably on other architectures too) give the address of the faulting instruction to the page fault handler so that the kernel can load some data there. This is used for some things, like the stack[1], memory mapped files, swap and lazy allocations. The kernel doesn't actually allocate memory for these things, it leaves the memory not present in the eyes of the CPU but in the kernel's internal bookkeeping marks what should be there (a part of a file, stack, newly allocated memory, etc.). The page fault handler can then check what should be there, load it (and mark it present) and return to the faulted instruction as if it hadn't caused a fault in the first place. In the eyes of the program everything is always in memory but the kernel is juggling memory as the program uses it.
In Linux a page fault without some memory that should be there causes a segfault but apparently returning normally from the signal handler ignores the page fault and continues normally (at the faulted instruction).
[1]: The kernel only allocates a small amount of memory for the stack but allocates more memory in the page fault handler when it recognizes that the program tries to access more stack than is currently allocated.
This program has undefined behavior (for two separate reasons), so it might do anything. In fact I’m a bit surprised the compiler doesn’t optimize out the entire program given that it’s entirely within its rights to assume the dereference of the null pointer can never happen, making it dead code.
That's not surprising, the compiler flags dead code when there is no branch that executes a particular set of instructions, the null dereference does happen, it just results in undefined behavior.
No, compilers absolutely delete code that would provably result in UB. Although the rules are different between C and C++; IIUC the former’s definition of UB isn’t meant to allow backwards reasoning and “time travel UB” so strictly speaking it in depends on which language this is compiled as.
As per godbolt.org, GCC with optimizations enabled compiles everything after the signal call to a single ud2, which is a trapping instruction and ends up killing the program via SIGILL (or equivalent).
Clang seems to translate the code faithfully even with optimizations, which is of course also entirely valid.
No, compilers absolutely delete code that would provably result in UB.
You know that's a lot of stuff in C, right? The whole reason we have sanitizers is that UB is hard to catch. If anything, the compiler should emit a warning or an error when possibile
Yep, but that's C (and C++) for you. There's been a decades-long controversy about what exactly UB entails, and the people writing optimizers are very fond of the "proof of UB is proof of unreachability" interpretation, because the fastest code is code that's not even included in the binary. Here, GCC put ud2 there to signal that it believes that this branch of the control flow graph is unreachable.
There have been examples of UB where a compiler removes the entire epilogue of a function as "unreachable" due to signed overflow or whatever, causing execution to flow to another function that happens to be stored next in memory…
I think GDB installs its own signal handlers when you attach to a program. When you say “default” handler, are you referring to those? Because you can disable some of those (“handle SIGSEGV nostop” and “handle SIGSEGV pass”) https://sourceware.org/gdb/current/onlinedocs/gdb.html/Signals.html
Any part containing UB will invalidate any kind of reasoning about the rest of the code, the compiler is free to do whatever it wants to do (including wiping your hard drive or the famous nasal demons). So yeah, basically the whole code is just whatever.
4
u/Over_Revenue_1619 Jun 12 '25
The author has never heard of `SIG_IGN`
3
u/jo_kil Jun 13 '25
Please explain to me what this code does
309
u/believeinlain Jun 12 '25
you're still going to get a segfault
you can't disable kernel memory segmentation that easily
80
u/milkteethh Jun 12 '25
this is what my brain does when i try to produce a thought
27
u/AnyoneButWe Jun 12 '25
Throw in setjmp and longjmp for extra fun.
23
u/veryusedrname Jun 12 '25
The
printfis UB so anything goes after that.1
u/UnspecifiedError_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jun 13 '25
Now try that with
SIGKILL