forked from shellphish/how2heap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc_playground.c
72 lines (69 loc) · 2.26 KB
/
malloc_playground.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <inttypes.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __GLIBC__
# include <malloc.h>
# include <mcheck.h>
void print_mcheck_status(enum mcheck_status s)
{
fprintf(stderr, "%s\n", (s == MCHECK_DISABLED) ? "N/A, you didn't enable mcheck()" :
(s == MCHECK_OK) ? "No inconsistency detected" :
(s == MCHECK_HEAD) ? "Memory preceding an allocated block was clobbered" :
(s == MCHECK_TAIL) ? "Memory following an allocated block was clobbered" :
(s == MCHECK_FREE) ? "A block of memory was freed twice" :
"unknown memory check code!");
}
void report_mcheck_fail(enum mcheck_status s)
{
fprintf(stderr, "*** PROGRAM WOULD ABORT: "); print_mcheck_status(s);
}
#endif
int main(int argc, char ** argv) {
fprintf(stderr, "pid: %d\n", getpid());
char buffer[1000];
while (1) {
fprintf(stderr, "> ");
fgets(buffer, sizeof(buffer), stdin);
char cmd[1000];
intptr_t arg1, arg2;
int num = sscanf(buffer, "%s %"SCNiPTR" %"SCNiPTR, cmd, &arg1, &arg2);
if (strcmp(cmd, "malloc") == 0) {
void* result = malloc(arg1);
fprintf(stderr, "==> %p\n", result);
} else if (strcmp(cmd, "free") == 0) {
free((void*) arg1);
fprintf(stderr, "==> ok\n");
} else if (strcmp(cmd, "show") == 0) {
if (num == 2) {
arg2 = 1;
}
long * src = (long*) arg1;
for (int i = 0; i < arg2; i++) {
fprintf(stderr, "%p: %#16.0lx\n", &src[i], src[i]);
}
#ifdef __GLIBC__
} else if (strcmp(cmd, "usable") == 0) {
fprintf(stderr, "usable size: %zu\n", malloc_usable_size((void*) arg1));
} else if (strcmp(cmd, "stats") == 0) {
malloc_stats();
} else if (strcmp(cmd, "info") == 0) {
malloc_info(0, stdout);
} else if (strcmp(cmd, "mcheck") == 0) {
fprintf(stderr, "==> %s\n", mcheck(report_mcheck_fail) == 0 ? "OK" : "ERROR");
} else if (strcmp(cmd, "mcheck_pedantic") == 0) {
fprintf(stderr, "==> %s\n", mcheck_pedantic(report_mcheck_fail) == 0 ? "OK" : "ERROR");
} else if (strcmp(cmd, "mprobe") == 0) {
if (num > 1) {
print_mcheck_status(mprobe((void*) arg1));
} else {
mcheck_check_all();
fprintf(stderr, "==> check_all ok\n");
}
#endif
} else {
puts("Commands: malloc n, free p, show p [n], usable p, stats, info, mprobe [p], mcheck, mcheck_pedantic");
}
}
}