Callbacks to Lisp from C (and SIGSEGV handling)
"J. Gareth Williams" <[email protected]> Sat, 22 Mar 2025 00:48:15 -0400
| Newsgroups | gmane.lisp.steel-bank.general |
|---|---|
| Message-ID | <CALfqwAo0OFGU0TYntX1Bzqm0WQe_4SUG2rUUFQGOGhFUW4UO2Q@mail.gmail.com> |
hi all,
I'm working on an application, putting together a libsbcl.so that calls a C
API to register callbacks back into Lisp. Environment is Debian, amd64,
SBCL 2.5.0. The C program is a proprietary binary, so I don't have control
over what it does.
I have these callbacks working, but after some number of calls, one will
fail with an unhandled SIGSEGV.
After some strace and Google, my understanding is that SBCL uses SIGSEGV in
its GC [1].
I think what is happening is that SBCL establishes its signal handlers
early on, and things work -- I see SIGSEGV signalled and handled with
`mprotect()` a few times:
rt_sigaction(SIGSEGV, {sa_handler=0x7ff7b76cfd10, sa_mask=[HUP INT QUIT
USR2 PIPE ALRM TERM CHLD TSTP URG XCPU XFSZ VTALRM PROF WINCH IO],
sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_NODEFER|SA_SIGINFO,
sa_restorer=0x7ff7b785b050}, {sa_handler=0x1964000, sa_mask=[SEGV],
sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7ff7b785b050}, 8) = 0
...
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_ACCERR, si_addr=0x52169910} ---
mprotect(0x52169000, 4096, PROT_READ|PROT_WRITE|PROT_EXEC) = 0
rt_sigreturn({mask=[HUP INT QUIT USR2 PIPE ALRM TERM CHLD TSTP URG XCPU
XFSZ VTALRM PROF WINCH IO]}) = 4128
But at some later time, the C binary sets its own signal handlers,
including SIGSEGV, and the next time SIGSEGV is signalled in SBCL as part
of its memory management activities, the expected handler isn't there to
gracefully implement the GC scheme:
rt_sigaction(SIGSEGV, {sa_handler=0x17876b0, sa_mask=[],
sa_flags=SA_RESTORER|SA_RESETHAND|0xffffffff00000000,
sa_restorer=0x7ff7b785b050}, NULL, 8) = 0
So my question is: how might I handle this? Is there a hook for me to
re-establish SBCL's handlers when I enter the callback? And maybe restore
the C side's handlers as I exit?
Would much appreciate any insight!
cheers,
gareth
[1]
https://medium.com/@MartinCracauer/generational-garbage-collection-write-barriers-write-protection-and-userfaultfd-2-8b0e796b8f7f
_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help