about summary refs log tree commit diff
path: root/frida_mode/src/stats/stats.c
blob: 1d3520bcd63ecfd1528e38c9a53150760a0539e8 (plain) (blame)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>

#include "frida-gumjs.h"

#include "config.h"
#include "entry.h"
#include "shm.h"
#include "stalker.h"
#include "stats.h"
#include "util.h"

#define MICRO_TO_SEC 1000000

char                *stats_filename = NULL;
guint64              stats_interval = 0;
static guint64       stats_interval_us = 0;
static int           stats_fd = -1;
static stats_data_t *stats_data = MAP_FAILED;

void stats_write(void) {

  if (stats_filename == NULL) { return; }

  if (stats_interval == 0) { return; }

  guint64 current_time = g_get_monotonic_time();
  if ((current_time - stats_data->prev.stats_time) < stats_interval_us) {

    return;

  }

  IGNORED_RETURN(ftruncate(stats_fd, 0));
  IGNORED_RETURN(lseek(stats_fd, 0, SEEK_SET));

  stats_data->curr.stats_time = current_time;

  GDateTime *date_time = g_date_time_new_now_local();
  char      *date_string = g_date_time_format(date_time, "%Y-%m-%d");
  char      *time_string = g_date_time_format(date_time, "%H:%M:%S");
  guint elapsed = (stats_data->curr.stats_time - stats_data->prev.stats_time) /
                  MICRO_TO_SEC;

  stats_print("stats\n");
  stats_print("-----\n");

  stats_print("%-21s %s %s\n", "Time", date_string, time_string);
  stats_print("%-30s %10u seconds \n", "Elapsed", elapsed);

  stats_print("\n");
  stats_print("\n");

  g_free(time_string);
  g_free(date_string);
  g_date_time_unref(date_time);

  stats_write_arch(stats_data);

  memcpy(&stats_data->prev, &stats_data->curr, sizeof(stats_t));

}

static void gum_afl_stalker_stats_increment_total(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.total++;

}

static void gum_afl_stalker_stats_increment_call_imm(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.call_imm++;

}

static void gum_afl_stalker_stats_increment_call_reg(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.call_reg++;

}

static void gum_afl_stalker_stats_increment_call_mem(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.call_mem++;

}

static void gum_afl_stalker_stats_increment_excluded_call_reg(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.excluded_call_reg++;

}

static void gum_afl_stalker_stats_increment_ret_slow_path(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.ret_slow_path++;

}

static void gum_afl_stalker_stats_increment_ret(GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.ret++;

}

static void gum_afl_stalker_stats_increment_post_call_invoke(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.post_call_invoke++;

}

static void gum_afl_stalker_stats_increment_excluded_call_imm(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.excluded_call_imm++;

}

static void gum_afl_stalker_stats_increment_jmp_imm(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_imm++;

}

static void gum_afl_stalker_stats_increment_jmp_reg(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_reg++;

}

static void gum_afl_stalker_stats_increment_jmp_mem(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_mem++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_imm(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_imm++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_mem(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_mem++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_reg(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_reg++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_jcxz(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_jcxz++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_cc(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_cc++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_cbz(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_cbz++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_cbnz(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_cbnz++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_tbz(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_tbz++;

}

static void gum_afl_stalker_stats_increment_jmp_cond_tbnz(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_cond_tbnz++;

}

static void gum_afl_stalker_stats_increment_jmp_continuation(
    GumStalkerObserver *observer) {

  UNUSED_PARAMETER(observer);

  if (!entry_compiled) { return; }
  stats_data->curr.jmp_continuation++;

}

static void stats_observer_init(GumStalkerObserver *observer) {

  GumStalkerObserverInterface *iface = GUM_STALKER_OBSERVER_GET_IFACE(observer);
  iface->increment_total = gum_afl_stalker_stats_increment_total;
  iface->increment_call_imm = gum_afl_stalker_stats_increment_call_imm;
  iface->increment_call_reg = gum_afl_stalker_stats_increment_call_reg;
  iface->increment_call_mem = gum_afl_stalker_stats_increment_call_mem;
  iface->increment_excluded_call_reg =
      gum_afl_stalker_stats_increment_excluded_call_reg;
  iface->increment_ret_slow_path =
      gum_afl_stalker_stats_increment_ret_slow_path;
  iface->increment_ret = gum_afl_stalker_stats_increment_ret;
  iface->increment_post_call_invoke =
      gum_afl_stalker_stats_increment_post_call_invoke;
  iface->increment_excluded_call_imm =
      gum_afl_stalker_stats_increment_excluded_call_imm;
  iface->increment_jmp_imm = gum_afl_stalker_stats_increment_jmp_imm;
  iface->increment_jmp_reg = gum_afl_stalker_stats_increment_jmp_reg;
  iface->increment_jmp_mem = gum_afl_stalker_stats_increment_jmp_mem;
  iface->increment_jmp_cond_imm = gum_afl_stalker_stats_increment_jmp_cond_imm;
  iface->increment_jmp_cond_mem = gum_afl_stalker_stats_increment_jmp_cond_mem;
  iface->increment_jmp_cond_reg = gum_afl_stalker_stats_increment_jmp_cond_reg;
  iface->increment_jmp_cond_jcxz =
      gum_afl_stalker_stats_increment_jmp_cond_jcxz;
  iface->increment_jmp_cond_cc = gum_afl_stalker_stats_increment_jmp_cond_cc;
  iface->increment_jmp_cond_cbz = gum_afl_stalker_stats_increment_jmp_cond_cbz;
  iface->increment_jmp_cond_cbnz =
      gum_afl_stalker_stats_increment_jmp_cond_cbnz;
  iface->increment_jmp_cond_tbz = gum_afl_stalker_stats_increment_jmp_cond_tbz;
  iface->increment_jmp_cond_tbnz =
      gum_afl_stalker_stats_increment_jmp_cond_tbnz;
  iface->increment_jmp_continuation =
      gum_afl_stalker_stats_increment_jmp_continuation;

}

void stats_config(void) {

  stats_filename = getenv("AFL_FRIDA_STATS_FILE");
  stats_interval = util_read_num("AFL_FRIDA_STATS_INTERVAL", 10);

}

void stats_init(void) {

  FOKF(cBLU "Stats" cRST " - " cGRN "file:" cYEL " [%s]",
       stats_filename == NULL ? " " : stats_filename);
  FOKF(cBLU "Stats" cRST " - " cGRN "interval:" cYEL " [%" G_GINT64_MODIFIER
            "u]",
       stats_interval);

  if (getenv("AFL_FRIDA_STATS_INTERVAL") != NULL &&
      getenv("AFL_FRIDA_STATS_FILE") == NULL) {

    FFATAL(
        "AFL_FRIDA_STATS_FILE must be specified if "
        "AFL_FRIDA_STATS_INTERVAL is");

  }

  stats_interval_us = stats_interval * MICRO_TO_SEC;

  if (stats_filename == NULL) { return; }

  char *path = g_canonicalize_filename(stats_filename, g_get_current_dir());

  FOKF(cBLU "Stats" cRST " - " cGRN "path:" cYEL " [%s]",
       path == NULL ? " " : path);

  stats_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
                  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

  if (stats_fd < 0) { FFATAL("Failed to open stats file '%s'", path); }

  g_free(path);

  GumStalkerObserver *observer = stalker_get_observer();
  stats_observer_init(observer);

  stats_data = shm_create(sizeof(stats_data_t));

  starts_arch_init();

}

void stats_print(char *format, ...) {

  char buffer[4096] = {0};
  int  len;

  va_list ap;
  va_start(ap, format);

  if (vsnprintf(buffer, sizeof(buffer) - 1, format, ap) < 0) { return; }

  len = strnlen(buffer, sizeof(buffer));
  IGNORED_RETURN(write(stats_fd, buffer, len));
  va_end(ap);

}

void stats_on_fork(void) {

  stats_write();

}

void stats_collect(const cs_insn *instr, gboolean begin) {

  if (!entry_compiled) { return; }
  if (stats_filename == NULL) { return; }
  stats_collect_arch(instr, begin);

}