title: How to handle SIGSEGV signal in userspace using Rust?
body: <p>I am trying to understand the stack overflow handler in Rust. I have written the function <code>recursive_stack()</code> which declares some local variables again and again to exhaust the stack space.</p>
  
  <pre><code>extern crate nix;
  
  use nix::sys::signal;
  
  extern "C" fn handle_sigsegv(_: i32) {
      //Do something here
  }
  
  fn main() {
      let sig_action = signal::SigAction::new(
          signal::SigHandler::Handler(handle_sigsegv),
          signal::SaFlags::SA_NODEFER,
          signal::SigSet::empty(),
      );
      unsafe {
          signal::sigaction(signal::SIGSEGV, &amp;sig_action);
      }
      println!("Before function");
      recursive_stack();
      println!("After function");
  }
  
  fn recursive_stack() {
      let _array: [i64; 50] = [0; 50];
      recursive_stack();
  }
  </code></pre>
  
  <p>I want to catch the signal and execute my signal handler. If I register my signal handler, I get a "Segmentation fault (core dumped)" message. If I don't register a signal handler than I get a stack overflow message.</p>
  
  <p>This signal handler works fine if I register it for <code>SIGINT</code> signal but gives strange results for <code>SIGSEGV</code>. What am I missing?</p>
  
  <p>I am running this program on Ubuntu 18.04. </p>