You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -423,6 +423,34 @@ This is much more efficient than pre-allocating large stacks for many fibers.
423
423
424
424
5. **Debugging** - Stack overflow in a fiber shows as segmentation fault, which is intentional
425
425
426
+
6. **Signal Handling** - To catch the `SIGSEGV` or `SIGBUS` generated by a stack overflow (guard page hit), you must register your signal handler with `SA_ONSTACK` and provide an alternate signal stack using `sigaltstack()`. Without this, the signal handler cannot run because the fiber's stack is invalid or exhausted.
427
+
428
+
```c
429
+
#include <signal.h>
430
+
431
+
void setup_signal_handling() {
432
+
/* 1. Allocate alternate stack */
433
+
stack_t ss;
434
+
ss.ss_sp = malloc(SIGSTKSZ);
435
+
ss.ss_size = SIGSTKSZ;
436
+
ss.ss_flags = 0;
437
+
if (sigaltstack(&ss, NULL) == -1) {
438
+
perror("sigaltstack");
439
+
exit(1);
440
+
}
441
+
442
+
/* 2. Register handler with SA_ONSTACK */
443
+
struct sigaction sa;
444
+
memset(&sa, 0, sizeof(sa));
445
+
sa.sa_handler = my_signal_handler;
446
+
sa.sa_flags = SA_ONSTACK; /* Run on alternate stack */
447
+
sigemptyset(&sa.sa_mask);
448
+
449
+
sigaction(SIGSEGV, &sa, NULL);
450
+
sigaction(SIGBUS, &sa, NULL);
451
+
}
452
+
```
453
+
426
454
## License
427
455
428
456
Derived from Boost.Context and DaoWen/fcontext, distributed under the Boost Software License 1.0.
0 commit comments