0

I'm trying to setup a SIGSEGV signal handler for Python using a C extension module:

import my_extension_module
import ctypes

my_extension_module.setup_segfault_handler()
ctypes.string_at(0) # i would like this to error and not segfault

I've seen several questions like this one and functions like longjmp or setjmp, but none of these seem to work how I intend.

I am aware that not terminating after a SIGSEGV is a bad idea for a number of reasons, but I need it nonetheless.

Here is my current code:

static jmp_buf buf;

void handle(int signum) {
  PyErr_SetString(PyExc_RuntimeError, "segmentation fault occured");
  longjmp(buf, 1);
}

static PyObject* method_setup() {
  signal(SIGSEGV, handle);
  setjmp(buf);

  Py_RETURN_NONE;
}
  • This is fraught with danger. I have no clue whether the Python interpreter will let you play games like this. _Why_ do you need it? – Paul Sanders May 20 '22 at 22:50
  • Segmentation violations are often a symptom of memory corruption. So it's rarely possible to do anything useful in the event handler. – Barmar May 20 '22 at 22:51
  • Can you elaborate on the "number of reasons" that you need to do this? – Joseph Sible-Reinstate Monica May 21 '22 at 00:47
  • You still haven't answered the question as to _why_ you _think_ you need this or _why_ you _think_ this will work? Even in pure C (where you have total control) , you can _not_ "recover" from this. See my answer: https://stackoverflow.com/questions/39431879/c-handle-signal-sigfpe-and-continue-execution/39432614#39432614https://stackoverflow.com/questions/39431879/c-handle-signal-sigfpe-and-continue-execution/39432614#39432614 as to why – Craig Estey May 21 '22 at 03:27

0 Answers0