This technically slows the main loop down a little, but makes it a lot less
machine-specific code.
-#define _GNU_SOURCE foo
-
#include <limits.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <limits.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
-#include <sys/ucontext.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX_BITS (sizeof(size_t) * CHAR_BIT - 1)
#include <unistd.h>
#define MAX_BITS (sizeof(size_t) * CHAR_BIT - 1)
+static ucontext_t ctx;
+
int dummy = 13;
int *pi;
static void catch_sigsegv(int signal, siginfo_t *info, void *p) {
int dummy = 13;
int *pi;
static void catch_sigsegv(int signal, siginfo_t *info, void *p) {
- (void)(signal);
-
- ucontext_t *uctx = (ucontext_t*)p;
- greg_t rip = uctx->uc_mcontext.gregs[REG_RIP];
+ (void)(signal); (void)(p);
fprintf(stderr, "Caught a segfault at %p\n", info->si_addr);
fflush(stderr);
fprintf(stderr, "Caught a segfault at %p\n", info->si_addr);
fflush(stderr);
- // Point it at a variable we can always load
- // Roll back %rip so we can reload *pi before dereferencing
- rip -= 7; // length of mov 0x2006b4(%rip),%rax # 0x6012b0 <pi>
- uctx->uc_mcontext.gregs[REG_RIP] = rip;
pi = (void*)(pvalue);
printf("*%p = ", (void*)pi);
fflush(stdout);
pi = (void*)(pvalue);
printf("*%p = ", (void*)pi);
fflush(stdout);
data = *pi;
if (pi != &dummy) {
printf("%04x\n", data);
data = *pi;
if (pi != &dummy) {
printf("%04x\n", data);
if (munmap((void*)(pvalue & ~0xFFFL), 4096)) {
perror("munmap");
}
if (munmap((void*)(pvalue & ~0xFFFL), 4096)) {
perror("munmap");
}
- } while (data == 0 || pi == &dummy);
+ } while (pi == &dummy || data == 0);