Created
June 9, 2018 19:20
-
-
Save apage43/275dba3d0747be3607fc82589446f35b 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 <sys/mman.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
size_t memtotal() { | |
char buf[200]; | |
size_t ret; | |
FILE* meminfo = fopen("/proc/meminfo", "r"); | |
if (!meminfo) { | |
perror("fopen"); | |
exit(1); | |
} | |
while(fgets(buf, sizeof(buf), meminfo)) { | |
int scan = sscanf(buf, "MemTotal: %zu kB", &ret); | |
if (scan == 1) { | |
ret = ret << 10; //kB -> B | |
goto cleanup; | |
} | |
} | |
exit(1); | |
cleanup: | |
fclose(meminfo); | |
return ret; | |
} | |
// The kernel won't hand us enough address space to consume all of the system's | |
// memory in one go, but it's happy enough to give us half that amount twice. | |
#define SPLIT 2 | |
int main(int argc, char** argv) { | |
int i; | |
size_t memsize = memtotal(); | |
void *blocks[SPLIT]; | |
printf("Total mem %zd bytes, let's get killed!\n", memsize); | |
for (i = 0; i < SPLIT; i++) { | |
void *blk = mmap(NULL, memsize / SPLIT, | |
PROT_WRITE | PROT_READ, | |
MAP_SHARED | MAP_ANONYMOUS, | |
-1, 0); | |
if (blk == MAP_FAILED) { | |
perror("mmap"); | |
exit(1); | |
} | |
blocks[i]=blk; | |
} | |
printf("Mapped enough address space to fill memory, now to touch it!\n"); | |
for (i = 0; i < SPLIT; i++) | |
memset(blocks[i], 0xff, memsize / SPLIT); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment