Created
March 11, 2016 20:46
-
-
Save chriswailes/147dcf1cad6971773626 to your computer and use it in GitHub Desktop.
Calculate the size of a memory page on your system.
This file contains hidden or 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
#define _GNU_SOURCE | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <ucontext.h> | |
volatile int done; | |
uint8_t * volatile top, | |
* volatile bottom, | |
* volatile iter, | |
* volatile iter_orig; | |
volatile int fault_count = 0; | |
static void segv_action(int sig, siginfo_t* info, void* arg_context) { | |
ucontext_t* context = (ucontext_t*)arg_context; | |
printf("Orig Iter: %p -- Iter: %p -- Fault Addr: %p\n", iter_orig, iter, info->si_addr); | |
done = 1; | |
iter = iter_orig; | |
if (top == NULL) { | |
top = info->si_addr; | |
} else if (bottom == NULL) { | |
bottom = info->si_addr + 1; | |
} | |
context->uc_mcontext.gregs[REG_RAX] = (long long)iter_orig; | |
if (++fault_count >= 4) { | |
exit(1); | |
} | |
} | |
int main(int argc, char** argv) { | |
struct sigaction action; | |
action.sa_handler = NULL; | |
action.sa_sigaction = segv_action; | |
action.sa_flags = SA_SIGINFO; | |
action.sa_restorer = NULL; | |
printf("System reports a page size of %d\n", getpagesize()); | |
sigaction(11, &action, NULL); | |
top = bottom = NULL; | |
iter_orig = iter = malloc(1); | |
done = 0; | |
while (0 == done) { | |
uint8_t x = *(iter++); | |
} | |
done = 0; | |
while (done == 0) { | |
uint8_t x = *(iter--); | |
} | |
printf("Top of page: %p\n", top); | |
printf("Bottom of page: %p\n", bottom); | |
printf("Calculated page size: 0x%lx\n", top - bottom); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment