-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.c
198 lines (163 loc) · 4.63 KB
/
hooks.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/******************************************/
#include "splinter.h"
/******************************************/
/*
* Not thread-safe, meant to be used from a single,
* worker thread. Otherwise, all synchronization
* has to be ensured at an upper layer.
*/
static hook_p __hooks_buffer = NULL;
static hook_p __hooks_free = NULL;
static hook_p __hooks_limbo = NULL;
uint_t stats_hooks_total = 0;
uint_t stats_hooks_free = 0;
uint_t stats_hooks_used = 0;
uint_t stats_hooks_limbo = 0;
uint_t stats_hooks_total_bytes = 0;
uint_t stats_hooks_free_bytes = 0;
uint_t stats_hooks_used_bytes = 0;
uint_t stats_hooks_limbo_bytes = 0;
static trigger_t __hook_alloc_trigger = NULL;
static trigger_t __hook_free_trigger = NULL;
static void __hook_free(hook_p h) {
uint32_t id;
DEBUG("freeing hook = %p", h);
atom_free(h->entry_chain);
atom_free(h->exit_chain);
id = h->id;
memset(h, 0, hook_s);
h->id = id;
h->next = __hooks_free;
__hooks_free = h;
stats_hooks_free += 1;
stats_hooks_free_bytes += hook_s;
if (__hook_free_trigger) __hook_free_trigger();
}
static int __hooks_gc(void) {
int still_in_limbo = 0;
hook_p prev = NULL;
hook_p curr = __hooks_limbo;
hook_p temp;
while(curr) {
DEBUG("hook = %p refcount = %u", curr, curr->refcount);
if (curr->refcount == 0) {
temp = curr->next;
if (curr == __hooks_limbo)
__hooks_limbo = temp;
if (prev)
prev->next = temp;
__hook_free(curr);
curr = temp;
stats_hooks_limbo -= 1;
stats_hooks_limbo_bytes -= hook_s;
} else {
still_in_limbo++;
prev = curr;
curr = curr->next;
}
}
return still_in_limbo;
}
int hooks_init(uint_t n, trigger_t alloc_trigger, trigger_t free_trigger) {
int i;
hook_p curr_hook;
debug(DEBUG_INF, "size = %lu", n);
if (!n) return hooks_cleanup();
if (__hooks_buffer) return -1;
if ((__hooks_buffer = splinter_memory_alloc(n * hook_s)) == NULL) {
debug(DEBUG_ERR, "could not alloc hooks buffer");
return -1;
}
for(i = 0, curr_hook = __hooks_buffer; i < n - 1; i++, curr_hook++) {
curr_hook->id = i + 1;
curr_hook->next = curr_hook + 1;
}
curr_hook->id = i + 1;
__hooks_free = __hooks_buffer;
stats_hooks_total = stats_hooks_free = n;
stats_hooks_total_bytes = stats_hooks_free_bytes = n * hook_s;
stats_hooks_used = stats_hooks_used_bytes = 0;
__hooks_limbo = NULL;
stats_hooks_limbo = stats_hooks_limbo_bytes = 0;
__hook_alloc_trigger = alloc_trigger;
__hook_free_trigger = free_trigger;
debug(DEBUG_DBG, "hooks buffer = %p - %p", __hooks_buffer, ((byte_p)__hooks_buffer) + n * hook_s);
return 0;
}
int hooks_cleanup() {
DEBUG();
if (!__hooks_buffer) return -1;
if (__hooks_gc()) return -1;
if (stats_hooks_free != stats_hooks_total) return -1;
memset(__hooks_buffer, 0, hook_s * stats_hooks_total);
__hooks_free = __hooks_limbo = __hooks_buffer =
splinter_memory_free(__hooks_buffer);
stats_hooks_total = stats_hooks_free = stats_hooks_used = stats_hooks_limbo = 0;
stats_hooks_total_bytes = stats_hooks_free_bytes =
stats_hooks_used_bytes = stats_hooks_limbo_bytes = 0;
__hook_alloc_trigger = NULL;
__hook_free_trigger = NULL;
return 0;
}
int hook_in_use(hook_p h) {
hook_p i;
DEBUG();
if (!h) return 0;
for(i = __hooks_free; i; i = i->next)
if (i == h) return 0;
for(i = __hooks_limbo; i; i = i->next)
if (i == h) return 0;
return 1;
}
hook_p hook_get(uint_t i) {
DEBUG();
if (!__hooks_buffer) return NULL;
__hooks_gc();
if (i == 0 || i > stats_hooks_total) return NULL;
return __hooks_buffer + i - 1;
}
hook_p hook_find(uint_t address) {
int i;
DEBUG();
if (!__hooks_buffer || !address) return NULL;
__hooks_gc();
for(i = 0; i < stats_hooks_total; i++) {
if (__hooks_buffer[i].address == address) return __hooks_buffer + i;
}
return NULL;
}
hook_p hook_alloc() {
hook_p h;
DEBUG();
if (!__hooks_buffer) return NULL;
__hooks_gc();
if ((h = __hooks_free) != NULL) {
__hooks_free = h->next;
stats_hooks_free -= 1;
stats_hooks_free_bytes -= hook_s;
stats_hooks_used += 1;
stats_hooks_used_bytes += hook_s;
if (__hook_alloc_trigger) __hook_alloc_trigger();
}
DEBUG("allocating hook = %p", h);
return h;
}
hook_p hook_free(hook_p h) {
DEBUG();
if (!h || !__hooks_buffer) return NULL;
__hooks_gc();
h->address = 0;
h->enabled = 0;
stats_hooks_used -= 1;
stats_hooks_used_bytes -= hook_s;
if (h->refcount == 0) {
__hook_free(h);
} else {
debug(DEBUG_INF, "sending %p to limbo", h);
h->next = __hooks_limbo;
__hooks_limbo = h;
stats_hooks_limbo += 1;
stats_hooks_limbo_bytes += hook_s;
}
return NULL;
}