News for hackers. Stuff that matters.
Re:Why isn't memset() async-signal-safe? by majke on Tuesday January 03, 2017 @06:07PM (#13314128)
Couple of things.

First, I don't think that's true nowadays. The discussion points that this (not restoring DF on signal return) is a kernel bug and should be fixed: https://lkml.org/lkml/2008/3/5/531

Second, the kernel tries to avoid doing too much work in the signal return code. It tries hard do _avoid_ heavy XSAVE and just preserve only the needed registers. This is the job of sigreturn(2) syscall btw. http://man7.org/linux/man-pages/man2/sigreturn.2.html

Third, there was a similar discussion about restoring SS (segment stack) register in x86-64 signal code. First patch proposed by Bryan Ford: https://lkml.org/lkml/2005/10/5/176 . Then 10 years later by Andy Lutomirski: https://lkml.org/lkml/2014/7/11/564

Last one was merged into mainline. Then reversed. Sadly. Then applied again: https://github.com/torvalds/linux/commit/6c25da5ad55d48c41b8...

The gist: if you modify SS register in the signal handler you are screwed. The only way around it is to install a trampoline using "the famous dosemu iret hack" described here:

http://www.x86-64.org/pipermail/discuss/2007-May/009913.html

(the site is down, can anyone find a mirror?)

On Linux use signalfd(2) whenever you can http://man7.org/linux/man-pages/man2/signalfd.2.html (Ie: put signal handling back in event loop). That's the only sane way of dealing with signals.

Re:Why isn't memset() async-signal-safe? by caf on Tuesday January 03, 2017 @07:08PM (#13314516)
The bug wasn't "not restoring DF on signal return" - it was always saved and restored correctly. The bug was not clearing DF on signal entry.
Re:Why isn't memset() async-signal-safe? by majke on Tuesday January 03, 2017 @07:56PM (#13314768)
agreed
Re:Why isn't memset() async-signal-safe? by amluto on Tuesday January 03, 2017 @07:39PM (#13314689)
> Last one was merged into mainline. Then reversed. Sadly. > Then applied again: https://github.com/torvalds/linux/commit/6c25da5ad55d48c41b8.... > > The gist: if you modify SS register in the signal handler you are screwed. The only way around it is to install a trampoline using "the famous dosemu iret hack" described here:

I'm not sure what you mean. This issue is fixed, so SS works exactly the way you would expect it to, unless you do very strange things indeed in which case you might need to fiddle with uc_flags.

Re:Why isn't memset() async-signal-safe? by majke on Tuesday January 03, 2017 @07:55PM (#13314763)
Strange or not, linux did not restore SS on sigreturn in 64 bit mode. The point of explaining that is to emphasize that the issue described in the blog post is not the only one in this code (the code doing entering / exiting signal handlers).

The issue is fixed indeed, as for Feb 17, 2016.

Re:Why isn't memset() async-signal-safe? by amluto on Wednesday January 04, 2017 @01:05AM (#13316024)
Okay, I read your post as meaning that it was still broken.

FWIW, I don't consider it sad that my first attempt was reverted. I inadvertently broke some assumptions that a real program (DOSEMU) was making, and Linux takes backwards compatibility quite seriously. The second version was better.

Re:Why isn't memset() async-signal-safe? by eridius on Tuesday January 03, 2017 @08:54PM (#13315048)
The site is down again? What's wrong with that site? A while back when I wanted to look something up in the x86_64 ABI the site was down too, for at least a week (I eventually gave up checking). At some point later it came back. I wonder how long it's been down this time.

Interestingly, Xcode's OS X docs used to link to x86-64.org for the AMD64 ABI, but looking right now, the link is removed (but the text remains), I guess because the site was down for so long.

Re:Why isn't memset() async-signal-safe? by hawkice on Tuesday January 03, 2017 @06:14PM (#13314177)
I mean, kernel bug or no, if that's how it actually works, then it isn't actually safe to be handle signals while executing these functions. If people are still working to change the code and standards we hold the code to, then you can't push changes that rely on the new behavior without introducing some truly bizarre race conditions.
Re:Why isn't memset() async-signal-safe? by majke on Tuesday January 03, 2017 @06:44PM (#13314391)
This comment suggests that #DF is rarely used anyway https://lkml.org/lkml/2008/3/6/50

> The conclusion is that DF=1 in x86_64 64-bit code is extremely rare

Re:Why isn't memset() async-signal-safe? by tedunangst on Tuesday January 03, 2017 @06:49PM (#13314418)
The bug is 8 years old. At some point you can assume it's been fixed.
Re:Why isn't memset() async-signal-safe? by hawkice on Tuesday January 03, 2017 @07:40PM (#13314698)
I mean, that's pretty optimistic, but perhaps: At some point you can assume that, if people get your updated code in 2017, they'll have an update from many years ago.

Depends on how releases are managed, but that might be much much more practical an assumption.