-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhook.c
89 lines (59 loc) · 1.8 KB
/
hook.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define _GNU_SOURCE
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define RESET "\x1b[0m"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <malloc.h>
#include <inttypes.h>
// gcc -o hook.so -fPIC -shared hook.c -ldl
// LD_PRELOAD=./hook.so ./myexecutable
// in GDB: set exec-wrapper env 'LD_PRELOAD=./hook.so'
#define BEGIN_HOOK \
reentrancy_guard++;
#define HOOK \
if (reentrancy_guard == 1)
#define END_HOOK \
reentrancy_guard--;
int reentrancy_guard;
void* (*real_malloc)(size_t size);
void* malloc(size_t size) {
BEGIN_HOOK
real_malloc = dlsym(RTLD_NEXT, "malloc");
void* chunk = real_malloc(size);
HOOK {
size_t s = malloc_usable_size(chunk);
fprintf(stderr, BLUE "MALLOC(%4ld): %p-%p (%4ld Bytes)\n" RESET, size, chunk, chunk+s, s);
}
END_HOOK
return chunk;
}
void (*real_free)(void* ptr);
void free(void* ptr) {
BEGIN_HOOK
real_free = dlsym(RTLD_NEXT, "free");
HOOK {
size_t s = malloc_usable_size(ptr);
fprintf(stderr, GREEN "FREE: %p-%p (%4ld Bytes)\n" RESET, ptr, ptr+s, s);
}
END_HOOK
return real_free(ptr);
}
void* (*real_realloc)(void* ptr, size_t size);
void* realloc(void* ptr, size_t size) {
BEGIN_HOOK
real_realloc = dlsym(RTLD_NEXT, "realloc");
void* old_chunk = ptr;
void* chunk = real_realloc(ptr, size);
HOOK {
size_t old_s = malloc_usable_size(old_chunk);
fprintf(stderr, YELLOW "REALLOC(%4ld): " GREEN "%p-%p (%4ld Bytes)\n" RESET, size, old_chunk, old_chunk+old_s, old_s);
size_t s = malloc_usable_size(chunk);
fprintf(stderr, YELLOW "'------------> " BLUE "%p-%p (%4ld Bytes)\n" RESET, chunk, chunk+s, s);
}
END_HOOK
return chunk;
}