News for hackers. Stuff that matters.
Re:Why isn't memset() async-signal-safe? by JoshTriplett on Tuesday January 03, 2017 @08:00PM (#13314796)
For trapping memory accesses, either something like userfaultfd, or otherwise handling segfaults via a signalfd from another thread with its own independent stack. (I'd also eliminate limitations about using a signalfd only within the same process that would have received the signals.)

Or, if you just need a "dirty page" bit, add a dedicated mechanism for that, which can use hardware features to run much faster without having to trap.

For debugging, we can do much better than ptrace. Linux already has dedicated syscalls to read and write another process's memory. Add some mechanisms to read and write registers, and extend the process stop/continue mechanism to allow single-stepping and stop-on-event (such as stop-on-syscall, or BPF-based filtering). I don't see any reason why debugging a process needs to incorporate signals.

Re:Why isn't memset() async-signal-safe? by gpderetta on Wednesday January 04, 2017 @03:07AM (#13316540)
>signalfd from another thread with its own independent stack.

This has similar reentrancy issues to signals.

Re:Why isn't memset() async-signal-safe? by JoshTriplett on Thursday January 05, 2017 @04:14AM (#13325823)
How so? The other thread would handle the signal from a well-defined point (reading the signalfd), and the thread that faulted would stop at the point of the fault until the thread processing the signal let it continue.
Re:Why isn't memset() async-signal-safe? by gpderetta on Thursday January 05, 2017 @04:56AM (#13325977)
What if the stopped thread was holding a mutex? The thread handling the signalfd can only safely call async signal safe functions, exactly like a signal handler.

edit: technically of course full reentrancy (implied by async signal safety) is not strictly required, "only" a fully non-blocking implementation of every function called by the signalfd thread.

Re:Why isn't memset() async-signal-safe? by JoshTriplett on Thursday January 05, 2017 @03:38PM (#13331136)
Ah, I see your concern. Right, if you called a function that took locks and then faulted, the thread handling the fault can't attempt to take the same locks. That should result in far fewer restrictions than "async-signal-safe", though.
Re:Why isn't memset() async-signal-safe? by gpderetta on Thursday January 05, 2017 @09:54PM (#13333546)
You do not know which lock it was holding though; when handling a segfault for example you need to be pessimistic and assume that you can't touch any lock (think of the allocator lock for example).

Async signal safety implies both reentrancy and non blocking algorithms [1]. You might not need reentrancy but you do need non-blocking. That's really a significant restriction as libraries with non-blocking guarantees are rare.

Re:Why isn't memset() async-signal-safe? by JoshTriplett on Friday January 06, 2017 @03:22AM (#13334734)
Depending on the nature of your segfault handler, you could either make sure you have any data structures you need already allocated, or allocate out of a separate arena. Or, alternatively, handle the signal in another process entirely.
Re:Why isn't memset() async-signal-safe? by gpderetta on Friday January 06, 2017 @05:36AM (#13335326)
I.e. it is safe as long as you follow the same rules you would use in a normal signal handler :).

Out of process handling is a robust solution though.