Created
July 21, 2014 16:01
-
-
Save scottt/a6cb65fc66f3e8fda7eb to your computer and use it in GitHub Desktop.
Test the GLIBC MALLOC_PERTURB_ feature
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
| #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