Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playing with a segfault #23

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions programming_languages/cpp/segfault/x86_64_recovery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "signal.h"
#include <vector>
#include <iostream>

// TODO(gitbuda): Try to make the following work on x86_64
// https://feepingcreature.github.io/handling.html

void seghandle_userspace() {
*(int*) NULL = 0;
}

// TODO(gitbuda): Make it complete and correct.
enum X86Registers {
RSP = 15,
RIP = 16,
};

void seghandle(int sig, siginfo_t* si, void* unused) {
ucontext_t* uc = (ucontext_t*) unused;
auto gregs = uc->uc_mcontext.gregs;
void* eip = (void*) gregs[X86Registers::RIP];
void** esp = (void**) gregs[X86Registers::RSP];
esp --;
*esp = eip;
eip = (void*) &seghandle_userspace;
}

void setup_segfault_handler() {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset (&sa.sa_mask);
sa.sa_sigaction = &seghandle;
if (sigaction(SIGSEGV, &sa, NULL) == -1) {
fprintf(stderr, "failed to setup SIGSEGV handler\n");
std::exit(1);
}
}

int main() {
// TODO(gitbuda): The program hangs figure out why :thinking:
setup_segfault_handler();
std::vector<int> nums;
std::cout << nums[1] << std::endl;
return 0;
}