Why isn't memset() async-signal-safe?
boston.conman.org
Why isn't memset() async-signal-safe?
1–10 of 98 posts
Re: Why isn't memset() async-signal-safe?
#2Seems like a good argument against an architecture having stateful flags that affect the execution of other instructions. Or, at least, against having such flags and not including them in the state saved and restored when switching contexts (including to signals).
Re: Why isn't memset() async-signal-safe?
#3[deleted]
Re: Why isn't memset() async-signal-safe?
#4Something that's always baffled me: why don't CPUs have a "save ALL state" and "restore ALL state" instructions? Why does every new set of CPU registers seem to require an OS update to save them on context switches?
Re: Why isn't memset() async-signal-safe?
#5Post title reminds me of "Hollywoo Stars and Celebrities: What Do They Know? Do They Know Things?? Let's Find Out!"[0]
[0] http://bojackhorseman.wikia.com/wiki/Hollywoo_Stars_and_Cele...!
Edit: Has been updated
Re: Why isn't memset() async-signal-safe?
#6Tricky! Even writing your own memset would experience the same behavior, since the compiler will assume the direction flag is unset.
I agree with colanderman; the kernel should be saving every register when the signal handler is entered.
Re: Why isn't memset() async-signal-safe?
#7I like this site, it's like one of those really classic hacker (in the woz sense) sites from 1995.
Re: Why isn't memset() async-signal-safe?
#8Related: Unix signals are deemed "unfixable" by some: https://lwn.net/Articles/414618/
Re: Why isn't memset() async-signal-safe?
#9Something that's always baffled me: why don't CPUs have a "save ALL state" and "restore ALL state" instructions? Why does every new set of CPU registers seem to require an OS update to save them on context switches?
They do, now. On current Intel CPUs, you can use xsave and xrstor to save and load the complete state, including all new state information. Ring 0 code can ask the CPU for the size of that state (via CPUID leaf 0xd), and allocate the appropriate amount of space per task.
Re: Why isn't memset() async-signal-safe?
#10The post makes it a bit unclear, so for the record: there's no theoretical reason the direction flag should be a problem. Just like any other callee-saved register, the kernel needs to save it to the stack and restore it when returning from the signal handler; there's nothing about the direction flag that makes it harder to do so, even if a signal handler is interrupting another signal handler or whatnot. And before branching to the signal handler function, just like it sets the registers used for function parameters to the correct values for the function signature, just like it aligns the stack pointer, the kernel (or a libc stub) needs to set the direction flag to 0. This is all defined by the ABI specification.
It's just that some kernels fail to do this (or did in the past), probably because the direction-flag requirement is less well known and most programs won't crash if it's neglected.