-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_generator.c
327 lines (272 loc) · 8.45 KB
/
window_generator.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define DEBUG
#define INSN_CLASS_MASK 0x07
#define BPF_JMP 0x05
#define BPF_JMP32 0x06
#define BPF_LD 0x00
#define BPF_LDX 0x01
#define BPF_ST 0x02
#define BPF_STX 0x03
#define MIN_WINDOW_SIZE 5
#define MAX_WINDOW_SIZE 10
struct window {
int l;
int r;
int heuristic_cost;
struct window *next;
};
unsigned char bitmap_get(unsigned char bitmap[], size_t n) {
size_t a = n / 8;
size_t b = n % 8;
return (bitmap[a] >> b) & 1;
}
void bitmap_set(unsigned char bitmap[], size_t n) {
size_t a = n / 8;
size_t b = n % 8;
bitmap[a] |= 1 << b;
}
void bitmap_clear(unsigned char bitmap[], size_t n) {
size_t a = n / 8;
size_t b = n % 8;
bitmap[a] &= ~(1 << b);
}
struct window *split(struct window *head) {
struct window *fast = head;
struct window *slow = head;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
if (fast != NULL) {
slow = slow->next;
}
}
struct window* temp = slow->next;
slow->next = NULL;
return temp;
}
struct window* merge(struct window* first, struct window* second) {
if (first == NULL) return second;
if (second == NULL) return first;
if (first->heuristic_cost >= second->heuristic_cost) {
first->next = merge(first->next, second);
return first;
} else {
second->next = merge(first, second->next);
return second;
}
}
struct window* merge_sort(struct window* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct window* second = split(head);
head = merge_sort(head);
second = merge_sort(second);
return merge(head, second);
}
int main(int argc, char *argv[])
{
// has optional arg of max_windows; otherwise uses default
if (argc != 2 && argc != 3) {
fprintf(stderr, "USAGE: %s </path/to/insns.txt>\n", argv[0]);
return EXIT_FAILURE;
}
int max_windows = -1;
if (argc == 3) {
max_windows = atoi(argv[2]);
if (max_windows == 0) {
max_windows = -1;
}
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
return EXIT_FAILURE;
}
char *line = NULL;
size_t n = 0;
size_t num_insns = 0;
while (getline(&line, &n, file) > 0) {
++num_insns;
}
// if no insns, then nothing to do.
if (num_insns == 0) {
fclose(file);
return EXIT_FAILURE;
}
size_t bitmap_size = (num_insns + 7) / 8;
int cost_map[num_insns]; // Populate with prefix traversal
memset(cost_map, 0, num_insns);
num_insns = 0;
unsigned char bitmap[bitmap_size];
memset(bitmap, 0, bitmap_size);
ssize_t len = 0;
rewind(file);
while ((len = getline(&line, &n, file)) > 0) {
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
unsigned char code, src_reg, dst_reg;
signed short off;
signed int imm;
if (sscanf(line, "{%hhu %hhu %hhu %hd %d}", &code, &src_reg, &dst_reg, &off, &imm) != 5) {
fprintf(stderr, "INVALID INSTRUCTION FORMAT: \"%s\"\n", line);
if (num_insns > 0) {
cost_map[num_insns] = cost_map[num_insns-1];
}
++num_insns;
continue;
}
int insn_class = code & INSN_CLASS_MASK;
unsigned char is_preferred = 0;
if (insn_class == BPF_JMP || insn_class == BPF_JMP32) {
int jmp_src = num_insns;
int jmp_dst;
// Positive offset
//if (off >= 0) {
// jmp_dst = jmp_src + off + 1;
//// Negative offset
//// TODO: ensure these are handled well
//} else {
// jmp_dst = jmp_src + off;
//}
jmp_dst = jmp_src + off + 1;
bitmap_set(bitmap, jmp_src); // JMP_SRC
bitmap_set(bitmap, jmp_dst); // JMP_DST
} else if (insn_class == BPF_LD || insn_class == BPF_LDX ||
insn_class == BPF_ST || insn_class == BPF_STX) {
is_preferred = 1;
}
if (num_insns == 0) {
cost_map[0] = is_preferred;
} else {
cost_map[num_insns] = cost_map[num_insns-1] + is_preferred;
}
++num_insns;
}
#ifdef DEBUG
FILE *debug = fopen("debug.txt", "w");
for (size_t i = 0; i < num_insns; ++i) {
fprintf(debug, "%d\n", bitmap_get(bitmap, i));
}
fclose(debug);
#endif
free(line);
fclose(file);
size_t num_windows = 0;
size_t left_ptr = 0, right_ptr = 0;
struct window *head = NULL;
struct window *tail = NULL;
while (right_ptr < num_insns) {
if (bitmap_get(bitmap, right_ptr) == 1) {
if (left_ptr == right_ptr) {
++right_ptr;
++left_ptr;
continue;
}
int size = right_ptr-left_ptr;
// Window must be within the size we're interested in
if (size >= MIN_WINDOW_SIZE && size <= MAX_WINDOW_SIZE) {
++num_windows;
struct window *current;
if (head == NULL) {
head = malloc(sizeof(struct window));
current = head;
} else {
current = malloc(sizeof(struct window));
tail->next = current;
}
current->l = left_ptr;
current->r = right_ptr-1;
// determine cost
if (left_ptr == 0) {
current->heuristic_cost = cost_map[right_ptr-1];
} else {
current->heuristic_cost = cost_map[right_ptr-1] - cost_map[left_ptr-1];
}
////
tail = current;
current->next = NULL;
}
++right_ptr;
left_ptr = right_ptr;
} else {
++right_ptr;
}
}
// add window at very end if not 1 (i.e. dangling window)
if (bitmap_get(bitmap, right_ptr - 1) == 0) {
int size = right_ptr-left_ptr;
// Window must be within the size we're interested in
if (size >= MIN_WINDOW_SIZE && size <= MAX_WINDOW_SIZE) {
++num_windows;
struct window *current;
if (head == NULL) {
head = malloc(sizeof(struct window));
current = head;
} else {
current = malloc(sizeof(struct window));
tail->next = current;
}
current->l = left_ptr;
current->r = right_ptr-1;
// determine cost
if (left_ptr == 0) {
current->heuristic_cost = cost_map[right_ptr-1];
} else {
current->heuristic_cost = cost_map[right_ptr-1] - cost_map[left_ptr-1];
}
////
tail = current;
current->next = NULL;
}
}
if (num_windows == 0) {
#ifdef DEBUG
printf("NO WINDOWS FOUND\n");
#endif
return EXIT_FAILURE;
}
// cap number of windows to user-specified amount
if (max_windows != -1 && max_windows < num_windows) {
num_windows = max_windows;
}
head = merge_sort(head);
struct window* temp = head;
char *start_window_buf = malloc(4096);
char *end_window_buf = malloc(4096);
start_window_buf[0] = '\0';
end_window_buf[0] = '\0';
int i = 0;
//FILE *output = fopen("output.txt", "w");
while (i < num_windows) {
int l=temp->l;
int r=temp->r;
char l_str[32], r_str[32];
sprintf(l_str, "%d", l);
sprintf(r_str, "%d", r);
strcat(start_window_buf, l_str);
strcat(end_window_buf, r_str);
if (i < num_windows - 1) {
strcat(start_window_buf, ",");
strcat(end_window_buf, ",");
}
temp = temp->next;
++i;
}
//fprintf(output, "--win_s_list %s --win_e_list %s", start_window_buf, end_window_buf);
printf("--win_s_list %s --win_e_list %s", start_window_buf, end_window_buf);
free(start_window_buf);
free(end_window_buf);
//fclose(output);
// free linked list at the very end
while (head != NULL) {
#ifdef DEBUG
printf("[%d,%d]=%d\n", head->l, head->r, head->heuristic_cost);
#endif
struct window *next = head->next;
free(head);
head = next;
}
return EXIT_SUCCESS;
}