Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
willmafh opened this issue Nov 29, 2018 · 2 comments
Assignees
Labels
answered question Further information is requested

Comments

@willmafh
Copy link

libaco/acosw.S

Line 65 in c941da6

mov ecx,DWORD PTR [esp+0x8] // to_co

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

@hnes hnes changed the title 直接通过esp取to_co地址会有问题吧??? Question: Would there be some problem if one signal handler is triggered during the call of acosw? Nov 30, 2018
@hnes hnes self-assigned this Nov 30, 2018
@hnes hnes added the question Further information is requested label Nov 30, 2018
@hnes
Copy link
Owner

hnes commented Nov 30, 2018

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
Copy link
Owner

hnes commented Nov 30, 2018

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 :-)

@hnes hnes added the answered label Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants