How to handle SIGSEGV signal in userspace using Rust?

你离开我真会死。 提交于 2019-12-23 21:44:59

问题


I am trying to understand the stack overflow handler in Rust. I have written the function recursive_stack() which declares some local variables again and again to exhaust the stack space.

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, &sig_action);
    }
    println!("Before function");
    recursive_stack();
    println!("After function");
}

fn recursive_stack() {
    let _array: [i64; 50] = [0; 50];
    recursive_stack();
}

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.

This signal handler works fine if I register it for SIGINT signal but gives strange results for SIGSEGV. What am I missing?

I am running this program on Ubuntu 18.04.

来源:https://stackoverflow.com/questions/52065965/how-to-handle-sigsegv-signal-in-userspace-using-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!