about summary refs log tree commit diff
path: root/custom_mutators/gramatron/gramfuzz-helpers.c
blob: f894c850efd9ea186fa7353ba9aa52e2db1df908 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "afl-fuzz.h"
#include "gramfuzz.h"

/*Slices from beginning till idx*/
Array *slice(Array *input, int idx) {

  // printf("\nSlice idx:%d", idx);
  terminal *origptr;
  terminal *term_ptr;
  Array *   sliced = (Array *)malloc(sizeof(Array));
  initArray(sliced, input->size);
  // Populate dynamic array members
  if (idx == 0) { return sliced; }
  for (int x = 0; x < idx; x++) {

    origptr = &input->start[x];
    insertArray(sliced, origptr->state, origptr->symbol, origptr->symbol_len,
                origptr->trigger_idx);

  }

  return sliced;

}

/* Slices from idx till end*/
Array *slice_inverse(Array *input, int idx) {

  // printf("\nSlice idx:%d", idx);
  terminal *origptr;
  terminal *term_ptr;
  Array *   sliced = (Array *)malloc(sizeof(Array));
  initArray(sliced, input->size);
  for (int x = idx; x < input->used; x++) {

    origptr = &input->start[x];
    insertArray(sliced, origptr->state, origptr->symbol, origptr->symbol_len,
                origptr->trigger_idx);

  }

  return sliced;

}

/*Carves with `start` included and `end` excluded*/
Array *carve(Array *input, int start, int end) {

  terminal *origptr;
  terminal *term_ptr;
  Array *   sliced = (Array *)malloc(sizeof(Array));
  initArray(sliced, input->size);
  for (int x = start; x < end; x++) {

    origptr = &input->start[x];
    insertArray(sliced, origptr->state, origptr->symbol, origptr->symbol_len,
                origptr->trigger_idx);

  }

  return sliced;

}

/*Concats prefix + feature *mult*/
void concatPrefixFeature(Array *prefix, Array *feature) {

  // XXX: Currently we have hardcoded the multiplication threshold for adding
  // the recursive feature. Might want to fix it to choose a random number upper
  // bounded by a static value instead.
  terminal *featureptr;
  int       len = rand() % RECUR_THRESHOLD;
  for (int x = 0; x < len; x++) {

    for (int y = 0; y < feature->used; y++) {

      featureptr = &feature->start[y];
      insertArray(prefix, featureptr->state, featureptr->symbol,
                  featureptr->symbol_len, featureptr->trigger_idx);

    }

  }

}

void concatPrefixFeatureBench(Array *prefix, Array *feature) {

  // XXX: Currently we have hardcoded the multiplication threshold for adding
  // the recursive feature. Might want to fix it to choose a random number upper
  // bounded by a static value instead.
  terminal *featureptr;
  int       len =
      5;  // 5 is the number of times we compare performing random recursion.
  for (int x = 0; x < len; x++) {

    for (int y = 0; y < feature->used; y++) {

      featureptr = &feature->start[y];
      insertArray(prefix, featureptr->state, featureptr->symbol,
                  featureptr->symbol_len, featureptr->trigger_idx);

    }

  }

}

Array *spliceGF(Array *orig, Array *toSplice, int idx) {

  terminal *toSplicePtr;
  terminal *tempPtr;
  // Iterate through the splice candidate from the `idx` till end
  for (int x = idx; x < toSplice->used; x++) {

    toSplicePtr = &toSplice->start[x];
    insertArray(orig, toSplicePtr->state, toSplicePtr->symbol,
                toSplicePtr->symbol_len, toSplicePtr->trigger_idx);

  }

  return orig;

}

Array *gen_input(state *pda, Array *input) {

  state *   state_ptr;
  trigger * trigger_ptr;
  terminal *term_ptr;
  int       offset = 0;
  int       randval, error;
  // Generating an input for the first time
  if (input == NULL) {

    input = (Array *)calloc(1, sizeof(Array));
    initArray(input, INIT_SIZE);
    curr_state = init_state;

  }

  while (curr_state != final_state) {

    // Retrieving the state from the pda
    state_ptr = pda + curr_state;

    // Get a random trigger
    randval = rand() % (state_ptr->trigger_len);
    trigger_ptr = (state_ptr->ptr) + randval;

    // Insert into the dynamic array
    insertArray(input, curr_state, trigger_ptr->term, trigger_ptr->term_len,
                randval);
    curr_state = trigger_ptr->dest;
    offset += 1;

  }

  return input;

}

Array *gen_input_count(state *pda, Array *input, int *mut_count) {

  state *   state_ptr;
  trigger * trigger_ptr;
  terminal *term_ptr;
  int       offset = 0;
  int       randval, error;
  // Generating an input for the first time
  if (input == NULL) {

    input = (Array *)calloc(1, sizeof(Array));
    initArray(input, INIT_SIZE);
    curr_state = init_state;

  }

  while (curr_state != final_state) {

    *mut_count += 1;
    // Retrieving the state from the pda
    state_ptr = pda + curr_state;

    // Get a random trigger
    randval = rand() % (state_ptr->trigger_len);
    trigger_ptr = (state_ptr->ptr) + randval;

    // Insert into the dynamic array
    insertArray(input, curr_state, trigger_ptr->term, trigger_ptr->term_len,
                randval);
    curr_state = trigger_ptr->dest;
    offset += 1;

  }

  return input;

}

/*Creates a candidate from walk with state hashmap and
 * recursion hashmap
 */

Candidate *gen_candidate(Array *input) {

  terminal *  term_ptr;
  IdxMap_new *idxmapPtr;
  // Declare the State Hash Table
  IdxMap_new *idxmapStart =
      (IdxMap_new *)malloc(sizeof(IdxMap_new) * numstates);
  for (int x = 0; x < numstates; x++) {

    idxmapPtr = &idxmapStart[x];
    utarray_new(idxmapPtr->nums, &ut_int_icd);

  }

  char *     trigger;
  int        state;
  char *     key;
  Candidate *candidate = (Candidate *)malloc(sizeof(Candidate));
  candidate->walk = input;
  int offset = 0, error;

  // Generate statemap for splicing
  while (offset < input->used) {

    term_ptr = &input->start[offset];
    state = term_ptr->state;
    // char *statenum = state + 1;
    // int num = atoi(statenum);
    idxmapPtr = &idxmapStart[state];
    utarray_push_back(idxmapPtr->nums, &offset);
    offset += 1;

  }

  candidate->statemap = idxmapStart;
  return candidate;

}

char *get_state(char *trigger) {

  // Get the state from transition
  int trigger_idx = 0;
  printf("\nTrigger:%s", trigger);
  char *state = (char *)malloc(sizeof(char) * 10);
  while (trigger[trigger_idx] != '_') {

    state[trigger_idx] = trigger[trigger_idx];
    trigger_idx += 1;

  }

  printf("\nTrigger Idx:%d", trigger_idx);
  state[trigger_idx] = '\0';
  return state;

}

void print_repr(Array *input, char *prefix) {

  size_t    offset = 0;
  terminal *term_ptr;
  char      geninput[input->used * 100];
  if (!input->used) {

    printf("\n=============");
    printf("\n%s:%s", prefix, "");
    printf("\n=============");
    return;

  }

  // This is done to create a null-terminated initial string
  term_ptr = &input->start[offset];
  strcpy(geninput, term_ptr->symbol);
  offset += 1;

  while (offset < input->used) {

    term_ptr = &input->start[offset];
    strcat(geninput, term_ptr->symbol);
    offset += 1;

  }

  printf("\n=============");
  printf("\n%s:%s", prefix, geninput);
  printf("\n=============");

}

// int main(int argc, char*argv[]) {

//     char *mode;
//     if (argc == 1) {

//         printf("\nUsage: ./gramfuzzer <mode>");
//         return -1;
//     }
//     if (argc >= 2) {

//         mode = argv[1];
//         printf("\nMode:%s", mode);
//     }
//     if (! strcmp(mode, "Generate")) {

//         GenInputBenchmark();
//     }
//     else if (! strcmp(mode, "RandomMutation")) {

//         RandomMutationBenchmark();
//     }
//     else if (! strcmp(mode, "Splice")) {

//         SpliceMutationBenchmark();
//     }
//     else if (! strcmp(mode, "Recursive")) {

//         RandomRecursiveBenchmark();
//     }
//     else {

//         printf("\nUnrecognized mode");
//         return -1;
//     }
//     return 0;
// }