News for hackers. Stuff that matters.
Re:Why isn't memset() async-signal-safe? by kazinator on Tuesday January 03, 2017 @06:52PM (#13314439)
This article seems flat out wrong. Since the direction flag is correctly saved and restored, everything is cool.

When some code is interrupted and the signal or interrupt handler calls memmove, that memmove will set up the direction flag for itself correctly. Its entire execution is nested within the handler. If it is interrupted by a nested interrupt, that nested one will restore the flag.

Now if an implementation of the memset function happens not to care about that flag, so that it changes direction from call to call, that's not a signal or interrupt problem! The flag can have arbitrary value in on entry to memcpy in an ordinary situation not involving threads or signals.

The moral is: never use the looping primitives on Intel without setting up the direction flag, if you care about reproducibility.

It goes without saying that the flag is part of the machine state; any machine context saving mechanism (for async situations) is broken if it neglects that flag.

Re:Why isn't memset() async-signal-safe? by kazinator on Tuesday January 03, 2017 @07:47PM (#13314722)
Also, POSIX explicitly requires memcpy and memmove to be async-signal-safe. Claiming they are not is misinformation.

The list of async-signal-safe functions is available here:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2...

Re:Why isn't memset() async-signal-safe? by kelnos on Tuesday January 03, 2017 @08:13PM (#13314868)
Odd, 'man 7 signal' (http://man7.org/linux/man-pages/man7/signal.7.html) does not list memcpy or memmove in the list of safe functions.
Re:Why isn't memset() async-signal-safe? by spc476 on Tuesday January 03, 2017 @08:58PM (#13315066)
It appears that memset() was only added in 2016.
Re:Why isn't memset() async-signal-safe? by olegolegovich on Wednesday January 04, 2017 @02:29AM (#13316383)
> The flag can have arbitrary value in on entry to memcpy in an ordinary situation not involving threads or signals.

It can not have arbitrary value on entry. x86-64 ABI mandates that the flag is cleared before any function is called (3.2.1 Registers and the Stack Frame: "The direction flag in the %eflags register must be clear on function entry, and on function return.")

Re:Why isn't memset() async-signal-safe? by kazinator on Wednesday January 04, 2017 @06:35AM (#13317371)
OK there we go, then. A compiler that conforms to the ABI generates code which clears the flag, if necessary. Interrupts preserve the flag. Thus, the flag should not be surprisingly set on entry into memcpy. If so, there is a bug.
Re:Why isn't memset() async-signal-safe? by olegolegovich on Wednesday January 04, 2017 @08:03AM (#13317891)
> A compiler that conforms to the ABI generates code which clears the flag, if necessary.

I'm sorry, I do not follow. The conforming compiler expects that the functions are called with the clear flag. The non-conforming kernel on the other hand does not clear the flag before calling the signal handler which breaks it.

Re:Why isn't memset() async-signal-safe? by kazinator on Wednesday January 04, 2017 @08:57PM (#13324055)
But that's obviously a bug. ABI says, caller clears flag. Ergo, all situations that trigger function calls shall clear flag.