hnes / libaco

A blazing fast and lightweight C asymmetric coroutine library 💎 ⛅🚀⛅🌞
https://libaco.org
Apache License 2.0
3.51k stars 391 forks source link

Question: Would there be some problem if one signal handler is triggered during the call of `acosw`? #24

Open willmafh opened 5 years ago

willmafh commented 5 years ago

https://github.com/hnes/libaco/blob/c941da6bb94a3800bf014722b7a248795f2b7ee2/acosw.S#L65 按照我的理解,这一行之前的: lea ecx,[esp+0x4] // esp, 通过将栈顶指针暂时保存在ecx中是作为一个定点的作用吧,因为前面注释里已经说了,signal可能改变栈顶指针esp的值,那这一行又用esp本身地址+8的方式取得to_co的地址,那万一在这之前esp的值由于signal改变了,那此时ecx里保存的值就不是to_co的地址了吧,不知道我这样配合你上面的注释理解得对不对。探讨一下...

hnes commented 5 years ago

Hi @willmafh ;-)

Signals can interrupt processes [see signal(BA_OS)]. Functions called during signal handling have no unusual restrictions on their use of registers. Moreover, if a signal handling function returns, the process resumes its original execution path with registers restored to their original values. Thus, programs and compilers may freely use all registers without the danger of signal handlers changing their values.

-- abi386-4.pdf

So it would be fine as long as we comply with the constraint described in the "Stack Pointer - Miscellaneous - Proof of Correctness" part of README.md:

The stack pointer should always points to the end of the latest allocated stack frame.

P.S.

Here is also a detailed description about the execution of signal handlers:

https://stackoverflow.com/questions/6949025/how-are-asynchronous-signal-handlers-executed-on-linux

hnes commented 5 years ago

I also found the description about the execution of signal handler in the man2 page of sigreturn is wonderful:

If the Linux kernel determines that an unblocked signal is pending for a process, then, at the next transition back to user mode in that process (e.g., upon return from a system call or when the process is rescheduled onto the CPU), it creates a new frame on the user-space stack where it saves various pieces of process context (processor status word, registers, signal mask, and signal stack settings).

The kernel also arranges that, during the transition back to user mode, the signal handler is called, and that, upon return from the handler, control passes to a piece of user-space code commonly called the "signal trampoline". The signal trampoline code in turn calls sigreturn().

This sigreturn() call undoes everything that was done—changing the process's signal mask, switching signal stacks (see sigaltstack(2))—in order to invoke the signal handler. Using the information that was earlier saved on the user-space stack sigreturn() restores the process's signal mask, switches stacks, and restores the process's context (processor flags and registers, including the stack pointer and instruction pointer), so that the process resumes execution at the point where it was interrupted by the signal.

You also could choose to use gdb to see how signal handler is executing during the call of acosw (here is a demo may be helpful). Happy hacking :-)