Skip to content

Instantly share code, notes, and snippets.

@akkartik
Created May 7, 2019 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkartik/8a82115c8a356fc1aa0eb7917206ff22 to your computer and use it in GitHub Desktop.
Save akkartik/8a82115c8a356fc1aa0eb7917206ff22 to your computer and use it in GitHub Desktop.
Stride through the heap until segfault
// Linux only.
// gcc x.c && ./a.out
//
// Example run on a 64-bit system:
// 0x56339fd78000 0x56339f74601a
// 0x56339fd78000 0x56339f74601b
// 0x56339fd78000 0x56339f74601c
// 0x56339fd78000 0x56339f74601d
// 0x56339fd78000 0x56339f74601e
// ...
// 0x56339fd78000 0x56339f746ffc
// 0x56339fd78000 0x56339f746ffd
// 0x56339fd78000 0x56339f746ffe
// 0x56339fd78000 0x56339f746fff
// 0x56339fd78000 0x56339f747000
// zsh: segmentation fault ./a.out
#include<unistd.h>
#include<stdio.h>
char a = 0;
char end_of_data_segment = 0;
int main(void) {
int* program_break = sbrk(0);
char* curr = &end_of_data_segment;
while (1) {
printf("%p %p\n", program_break, curr);
fflush(stdout);
a = *curr;
++curr;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment