What is the use of SA_ONSTACK in sigaction?

When the signal is delivered, the signal handler runs on the process stack. If SA_ONSTACK is used in sigaction() , then another stack is used.

What is the use of using another stack? Any use case?

+4
source share
2 answers

One use of the alternative stack is to properly handle SIGSEGV .

If your process received only SIGSEGV because it exceeded the stack limit, you cannot start the signal handler in the process stack - it is already full. The presence of an alternative stack allows you to (cautiously) perform more or less graceful completion in this case.

+11
source

Another interesting example is when you associate β€œnormal” code, such as C, with some other version of the language using split stacks such as Go.

In Go, goroutines (light streams) have a rather small stack that expands as needed. Basically, each function prolog checks that the stack has enough free space, and increases the stack if it does not.

When Go calls C code through cgo, it automatically expands the stack to meet C expectations.

However, if the C code sets up signal handlers, they can be called at any time, including if there is not enough stack space.

Thus, any C code that references Go code must use SA_ONSTACK so as not to crash.

https://golang.org/pkg/os/signal/#hdr-Go_programs_that_use_cgo_or_SWIG

+1
source

Source: https://habr.com/ru/post/1399553/


All Articles