Skip to content

Instantly share code, notes, and snippets.

@scottt
Created July 21, 2014 16:01
Show Gist options
  • Select an option

  • Save scottt/a6cb65fc66f3e8fda7eb to your computer and use it in GitHub Desktop.

Select an option

Save scottt/a6cb65fc66f3e8fda7eb to your computer and use it in GitHub Desktop.
Test the GLIBC MALLOC_PERTURB_ feature
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <malloc.h>
struct Rect {
char a, b;
};
typedef struct Rect Rect;
enum {
N = 11,
};
void do_test(void)
{
int i;
Rect *p;
unsigned char *cp;
size_t k;
k = sizeof(typeof(*p)) * N;
cp = malloc(k);
p = (Rect*)cp;
printf("p: {%lld, %lld}\n", (long long)p->a, (long long)p->b);
for (i=0; i<k; i++)
printf("%02x", cp[i]);
putchar('\n');
free(p);
printf("p: {%lld, %lld}\n", (long long)p->a, (long long)p->b);
for (i=0; i<k; i++)
printf("%02x", cp[i]);
putchar('\n');
}
int main()
{
/* same as "export MALLOC_PERTURB_=0" */
mallopt(M_PERTURB, 0);
do_test();
/* same as "export MALLOC_PERTURB_=$((0x7f))" */
mallopt(M_PERTURB, 0x7f);
do_test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment