Created
May 20, 2015 16:31
-
-
Save wilcoxjay/50abdd1da3c5aa5d7ca3 to your computer and use it in GitHub Desktop.
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 "dbg.h" | |
#include <sys/mman.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
void* goal_addr = (void*)0x10000; | |
size_t length = 1 << 10; | |
// MAP_FIXED means "map at exactly the goal address". otherwise, the | |
// given address is used only as a "hint". this also means that if | |
// for whatever reason it's not possible to map at exactly the goal | |
// address, mmap will fail. | |
// MAP_ANONYMOUS means "don't memory map a file, just give me a | |
// bunch of memory". you can then pass -1 as the fd. | |
void* addr = mmap(goal_addr, length, | |
PROT_READ | PROT_WRITE, | |
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, | |
-1, 0); | |
// Be careful to check for MAP_FAILED instead of NULL | |
check(addr != MAP_FAILED, "mmap failed"); | |
// At this point, we're guaranteed to have memory available at | |
// addr. Just to be extra sure, we can assert that the memory is | |
// where we wanted it to be. | |
check(addr == goal_addr, "mmap succeeded, but with wrong address!"); | |
printf("mapped addres %p\n", addr); | |
// Now we can fiddle with the memory however we want. | |
char* c; | |
for (c = addr; c < ((char*)addr) + 10; c++) { | |
*c = ((long long)c & 0xff); | |
} | |
for (c = addr; c < ((char*)addr) + 10; c++) { | |
printf("wrote byte %d to location %p\n", *c, c); | |
} | |
return 0; | |
error: | |
exit(EXIT_FAILURE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment