Created
August 12, 2014 04:37
-
-
Save phire/d908ec9974821cd5cfed to your computer and use it in GitHub Desktop.
segfault benchmark.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/mman.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
void *mem; | |
#define MEM_SIZE (8000*0x1000) | |
inline double gettime () | |
{ | |
timeval tv; | |
gettimeofday (&tv, NULL); | |
return double (tv.tv_sec) + 0.000001 * tv.tv_usec; | |
} | |
typedef long unsigned int u64; | |
void sigsegv_handler(int sig, siginfo_t *info, void *raw_context) { | |
if(info->si_code != SEGV_ACCERR) | |
return; | |
ucontext_t *context = (ucontext_t *)raw_context; | |
mcontext_t *ctx = &context->uc_mcontext; | |
u64 bad_address = (u64)info->si_addr; | |
u64 bad_page = bad_address & (~0xfff); | |
mprotect((void *)bad_page, 0x1000, PROT_READ|PROT_WRITE); | |
} | |
void installHandler() { | |
struct sigaction sa; | |
sa.sa_handler = nullptr; | |
sa.sa_sigaction = &sigsegv_handler; | |
sa.sa_flags = SA_SIGINFO; | |
sigemptyset(&sa.sa_mask); | |
sigaction(SIGSEGV, &sa, nullptr); | |
} | |
int main(int argc, char **argv) { | |
installHandler(); | |
// allocate us some memory; | |
mem = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
if ((u64)mem == -1) { | |
perror("mmap"); | |
return -1; | |
} | |
mprotect(mem, MEM_SIZE, PROT_READ); | |
double start = gettime(); | |
for (int i = 0; i < 8000; i+= 1) | |
((unsigned char*)mem)[i*0x1000] = 0xcc; | |
double end = gettime(); | |
munmap(mem, MEM_SIZE); | |
printf("time: %gus\n", (end - start)*1000000); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
benchmark: main.cpp | |
g++ main.cpp -O3 -g -std=c++11 -o benchmark |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment