Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Created February 12, 2019 02:50
Show Gist options
  • Save Yepoleb/61b13de84852da8544989d9f2adcc116 to your computer and use it in GitHub Desktop.
Save Yepoleb/61b13de84852da8544989d9f2adcc116 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE 1
#include <ucontext.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void action_handler(int sig, siginfo_t* info, void* context_p)
{
ucontext_t* ucontext = (ucontext_t*)context_p;
ucontext->uc_mcontext.gregs[REG_RIP] += 6;
}
int test_pointer(const void* p);
__asm__(
".global test_pointer\n"
"test_pointer:\n"
"movb (%rdi), %al\n"
"movl $0, %eax\n"
"ret\n"
"movl $1, %eax\n"
"ret\n"
);
size_t strlen_segv(const char* str)
{
struct sigaction sa_set = {0};
sa_set.sa_sigaction = action_handler;
sa_set.sa_flags = SA_SIGINFO;
sigemptyset(&sa_set.sa_mask);
sigaction(SIGSEGV , &sa_set, NULL);
size_t index = 0;
while (1) {
int did_segfault = test_pointer(str + index);
if (did_segfault) {
break;
}
index++;
}
struct sigaction sa_unset = {0};
sa_unset.sa_handler = SIG_DFL;
sigemptyset(&sa_unset.sa_mask);
sigaction(SIGSEGV , &sa_unset, NULL);
return index;
}
int main()
{
char* str1 = calloc(20, 1);
size_t length = strlen_segv(str1);
printf("len: %zu\n", length);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment