-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathafl_fuzz.py
748 lines (549 loc) · 23.4 KB
/
afl_fuzz.py
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# Manul - AFL mutator ported to Python
# -------------------------------------
# Maksim Shudrak <mshudrak@salesforce.com> <mxmssh@gmail.com>
#
# Copyright 2019 Salesforce.com, inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from printing import *
import ast
#TODO: write unit test for each of this function.
tokens_list = None
tokens_list_length = None
def bitflip_1bit(data, func_state): # for i in range((len(data)*8)):
if not func_state:
func_state = 0
if func_state >= len(data) * 8:
return data, None # we are done here, lets switch to the next function
data[int(func_state / 8)] ^= (0x80 >> (func_state % 8))
func_state += 1
return data, func_state
def bitflip_2bits(data, func_state): # for i in range((len(data)*7)):
if not func_state:
func_state = 0
if func_state >= len(data) * 7:
return data, None# we are done here, lets switch to the next function
data[int(func_state / 7)] ^= (0xC0 >> (func_state % 7))
func_state += 1
return data, func_state
def bitflip_4bits(data, func_state): # for i in range((len(data)*5)):
if not func_state:
func_state = 0
if func_state >= len(data) * 5:
return data, None # we are done here, lets switch to the next function
data[int(func_state / 5)] ^= (0xF0 >> (func_state % 5))
func_state += 1
return data, func_state
def byteflip_1(data, func_state): # for i in range((len(data))):
if not func_state:
func_state = 0
if func_state >= len(data):
return data, None # we are done here, lets switch to the next function
data[func_state] ^= 0xFF
func_state += 1
return data, func_state
def byteflip_2(data, func_state): # for i in range(1, ((len(data)))):
if not func_state:
func_state = 0
if func_state + 1 >= len(data):
return data, None # we are done here, lets switch to the next function
if len(data) > 1:
data[func_state] ^= 0xFF
data[func_state + 1] ^= 0xFF
else:
return data, None # input too small for byteflipping
func_state += 1
return data, func_state
def byteflip_4(data, func_state):
if not func_state:
func_state = 0
if func_state + 3 >= len(data):
return data, None
if len(data) > 3:
data[func_state] ^= 0xFF
data[func_state + 1] ^= 0xFF
data[func_state + 2] ^= 0xFF
data[func_state + 3] ^= 0xFF
else:
return data, None # input too small for byteflipping
func_state += 1
return data, func_state
def mutate_byte_arithmetic(data, func_state):
if not func_state:
func_state = [0, 0, False]
if func_state[1] > AFL_ARITH_MAX:
func_state[0] += 1
func_state[1] = 0
if func_state[0] >= len(data):
if func_state[2] == False:
func_state = [0, 0, True]
else:
return data, None
# TODO: we have to check for could_be_bitflip()
if func_state[2] == False:
val = data[func_state[0]] + func_state[1]
else:
val = data[func_state[0]] - func_state[1]
store_8(data, func_state[0], val)
func_state[1] += 1
return data, func_state
def mutate_2bytes_arithmetic(data, func_state):
data_len = len(data)
if data_len < 2:
return data, None
if not func_state:
func_state = [0, 0, False]
if func_state[1] > AFL_ARITH_MAX:
func_state[0] += 1
func_state[1] = 0
if func_state[0] + 1 >= data_len:
if func_state[2] == False:
func_state = [0, 0, True]
else:
return data, None
# TODO: we have to check for could_be_bitflip()
val = load_16(data, func_state[0])
if func_state[2] == False:
val += func_state[1]
else:
val -= func_state[1]
store_16(data, func_state[0], val)
func_state[1] += 1
return data, func_state
def mutate_4bytes_arithmetic(data, func_state):
data_len = len(data)
if data_len < 4:
return data, None
if not func_state:
func_state = [0, 0, False]
if func_state[1] > AFL_ARITH_MAX:
func_state[0] += 1
func_state[1] = 0
if func_state[0] + 3 >= len(data):
if func_state[2] == False:
func_state = [0, 0, True]
else:
return data, None
# TODO: we have to check for could_be_bitflip()
val = load_32(data, func_state[0])
if func_state[2] == False:
val += func_state[1]
else:
val -= func_state[1]
store_32(data, func_state[0], val)
func_state[1] += 1
return data, func_state
# TODO: implement is_not_bitflip and is_not_arithmetic
def mutate_1byte_interesting(data, func_state):
if not func_state:
func_state = [0, 0]
if func_state[1] >= len(interesting_8_Bit):
func_state[0] += 1
func_state[1] = 0
if func_state[0] >= len(data):
return data, None
interesting_value = interesting_8_Bit[func_state[1]]
data[func_state[0]] = in_range_8(interesting_value)
func_state[1] += 1
return data, func_state
# TODO: implement is_not_bitflip and is_not_arithmetic
def mutate_2bytes_interesting(data, func_state):
data_len = len(data)
if data_len < 2:
return data, None
if not func_state:
func_state = [0, 0, False]
if func_state[1] >= len(interesting_16_Bit):
func_state[0] += 1
func_state[1] = 0
if func_state[0] + 1 >= data_len:
if func_state[2] == False:
func_state = [0, 0, True]
else:
return data, None
interesting_value = in_range_16(interesting_16_Bit[func_state[1]])
if func_state[2]:
interesting_value = swap_16(interesting_value)
store_16(data, func_state[0], interesting_value)
func_state[1] += 1
return data, func_state
# TODO: implement is_not_bitflip and is_not_arithmetic
def mutate_4bytes_interesting(data, func_state):
data_len = len(data)
if data_len < 4:
return data, None
if not func_state:
func_state = [0, 0, False]
if func_state[1] >= len(interesting_32_Bit):
func_state[0] += 1
func_state[1] = 0
if func_state[0] + 3 >= data_len:
if func_state[2] == False:
func_state = [0, 0, True]
else:
return data, None
interesting_value = in_range_32(interesting_32_Bit[func_state[1]])
if func_state[2]:
interesting_value = swap_32(interesting_value)
store_32(data, func_state[0], interesting_value)
func_state[1] += 1
return data, func_state
#TODO: auto-create dictionary from binary
#TODO: afl has this also https://github.com/mirrorer/afl/blob/2fb5a3482ec27b593c57258baae7089ebdc89043/afl-fuzz.c#L5123
def dictionary_overwrite(data, func_state):
global tokens_list, tokens_list_length
if tokens_list_length <= 0:
return data, None
if not func_state:
func_state = [0, 0] # first is an index in tokens_list, second is an index in data
data_len = len(data)
token = tokens_list[func_state[0]]
place = func_state[1]
if data_len < len(token):
return data, None
if place >= data_len - len(token):
func_state[0] += 1 # take the next token
func_state[1] = 0
if func_state[0] >= len(tokens_list):
return data, None
data = data[:place] + bytearray(token) + data[place + len(token):]
func_state[1] += 1
return data, func_state
def dictionary_insert(data, func_state):
global tokens_list, tokens_list_length
if tokens_list_length <= 0:
return data, None
if not func_state:
func_state = [0, 0] # first is an index in tokens_list, second is an index in data
data_len = len(data)
token = tokens_list[func_state[0]]
place = func_state[1]
if place >= data_len:
func_state[0] += 1 # take the next token
func_state[1] = 0
if func_state[0] >= len(tokens_list):
return data, None
data = data[:place] + bytearray(token) + data[place:]
func_state[1] += 1
return data, func_state
def havoc_bitflip(data):
value_to_flip = RAND(len(data) * 8)
data, res = bitflip_1bit(data, value_to_flip)
return data
def havoc_interesting_byte(data):
value_to_change = RAND(len(data))
interesting_value_index = RAND(len(interesting_8_Bit))
func_state = [value_to_change, interesting_value_index]
data, state = mutate_1byte_interesting(data, func_state)
return data
def havoc_interesting_2bytes(data):
data_len = len(data)
if data_len < 2:
return data
value_to_change = RAND(data_len - 1) # substract 1 to make sure we have space for 2 bytes
interesting_value_index = RAND(len(interesting_16_Bit))
swap = RAND(2) # is swap?
func_state = [value_to_change, interesting_value_index, swap]
data, state = mutate_2bytes_interesting(data, func_state)
return data
def havoc_interesting_4bytes(data):
data_len = len(data)
if data_len < 4:
return data
value_to_change = RAND(len(data) - 3) # substract 1 to make sure we have space for 2 bytes
interesting_value_index = RAND(len(interesting_32_Bit))
swap = RAND(2) # is swap?
func_state = [value_to_change, interesting_value_index, swap]
data, state = mutate_4bytes_interesting(data, func_state)
return data
def havoc_randomly_add(data): # similar to mutate_byte_arithmetic but a bit faster
value_to_change = RAND(len(data))
data[value_to_change] = in_range_8(data[value_to_change] + 1 + RAND(AFL_ARITH_MAX))
return data
def havoc_randomly_substract(data): # similar to mutate_byte_arithmetic but a bit faster
value_to_change = RAND(len(data))
data[value_to_change] = in_range_8(data[value_to_change] - (1 + RAND(AFL_ARITH_MAX)))
return data
def havoc_randomly_add_2bytes(data): # similar to mutate_byte_arithmetic but a bit faster
data_len = len(data)
if data_len < 2:
return data
func_state = [RAND(data_len - 1), RAND(AFL_ARITH_MAX), True] # pos, value, is_sub
data, func_state = mutate_2bytes_arithmetic(data, func_state)
return data
def havoc_randomly_substract_2bytes(data): # similar to mutate_byte_arithmetic but a bit faster
data_len = len(data)
if data_len < 2:
return data
func_state = [RAND(data_len - 1), RAND(AFL_ARITH_MAX), False] # pos, value, is_sub
data, func_state = mutate_2bytes_arithmetic(data, func_state)
return data
def havoc_randomly_add_4bytes(data): # similar to mutate_byte_arithmetic but a bit faster
data_len = len(data)
if data_len < 4:
return data
func_state = [RAND(data_len - 3), RAND(AFL_ARITH_MAX), True] # pos, value, is_sub
data, func_state = mutate_4bytes_arithmetic(data, func_state)
return data
def havoc_randomly_substract_4bytes(data): # similar to mutate_byte_arithmetic but a bit faster
data_len = len(data)
if data_len < 4:
return data
func_state = [RAND(data_len - 3), RAND(AFL_ARITH_MAX), False] # pos, value, is_sub
data, func_state = mutate_4bytes_arithmetic(data, func_state)
return data
def havoc_set_randomly(data):
pos = RAND(len(data))
data[pos] = in_range_8(data[pos] ^ (1 + RAND(255)));
return data
def havoc_remove_randomly_block(data):
data_len = len(data)
if data_len <= 2:
return data
len_to_remove = AFL_choose_block_len(data_len - 1)
pos = RAND(data_len - len_to_remove + 1)
data = data[:pos] + data[pos+len_to_remove:]
return data
def prepare_block(data):
actually_clone = RAND(4)
data_len = len(data)
if actually_clone:
clone_len = AFL_choose_block_len(data_len)
clone_from = RAND(data_len - clone_len + 1 )
else:
clone_len = AFL_choose_block_len(AFL_HAVOC_BLK_XL)
clone_from = 0
clone_to = RAND(data_len)
if actually_clone:
block = data[clone_from:clone_from + clone_len]
else:
use_data_block = RAND(2)
if use_data_block:
block_start = RAND(data_len)
block = data[block_start:block_start+clone_len]
else:
block = [RAND(256)] * clone_len # TODO: check if it is actually correct implementation
block = bytearray(block)
return block, clone_to, clone_len
# insert random block
def havoc_clone_randomly_block(data):
block, clone_to, clone_len = prepare_block(data)
if clone_len == 0:
return data
data = data[:clone_to] + block + data[clone_to:]
return data
# overwrite random block
def havoc_overwrite_randomly_block(data):
block, clone_to, clone_len = prepare_block(data)
if clone_len == 0:
return data
data = data[:clone_to] + block + data[clone_to+clone_len:]
return data
# overwrite from dict
def havoc_overwrite_with_dict(data):
func_state = [RAND(len(tokens_list)), RAND(len(data))]
data, func_state = dictionary_overwrite(data, func_state)
return data
# overwrite from dict
def havoc_insert_with_dict(data):
func_state = [RAND(len(tokens_list)), RAND(len(data))]
data, func_state = dictionary_insert(data, func_state)
return data
#TODO: https://github.com/mirrorer/afl/blob/2fb5a3482ec27b593c57258baae7089ebdc89043/afl-fuzz.c#L6478
def havoc(data, func_state, max_havoc_cycles):
if not func_state:
func_state = 0
if func_state >= max_havoc_cycles:
return data, None
# havoc_randomly used twice to increase chances
func_to_choose = [havoc_bitflip, havoc_interesting_byte, havoc_interesting_2bytes, havoc_interesting_4bytes,
havoc_randomly_add, havoc_randomly_substract, havoc_randomly_add_2bytes,
havoc_randomly_substract_2bytes, havoc_randomly_add_4bytes, havoc_randomly_substract_4bytes,
havoc_set_randomly, havoc_remove_randomly_block, havoc_remove_randomly_block, havoc_clone_randomly_block,
havoc_overwrite_randomly_block, havoc_overwrite_with_dict, havoc_insert_with_dict]
use_stacking = 1 << 1 + RAND(AFL_HAVOC_STACK_POW2)
for i in range (0, use_stacking):
method = RAND(len(func_to_choose))
# randomly select one of the available methods
data = func_to_choose[method](data)
func_state += 1
return data, func_state
def splice(data, list_of_files, queue_path, func_state, max_havoc_cycles):
data_len = len(data)
if data_len <= 2:
return data, None
if len(list_of_files) <= 1:
return data, None
if not func_state:
func_state = 0
if func_state > SPLICE_CYCLES:
return data, None
# pick up random file from queue and splice with it
file_id = RAND(len(list_of_files))
if isinstance(list_of_files[file_id], tuple):
file_name = list_of_files[file_id][1]
picked_file_name = queue_path + "/" + file_name
else:
# TODO: currently we don't touch original files (which is not right I believe)
del list_of_files[file_id]
return splice(data, list_of_files, queue_path, None, max_havoc_cycles)
content_target = extract_content(picked_file_name)
content_target_len = len(content_target)
if content_target_len < 2 or is_bytearrays_equal(data, content_target):
del list_of_files[file_id]
return splice(data, list_of_files, queue_path, None, max_havoc_cycles)
f_diff, l_diff = locate_diffs(data, content_target, MIN(data_len, content_target_len))
if l_diff < 2 or f_diff == l_diff: # afl has f_diff == 0 but I believe we want to start with 0
del list_of_files[file_id]
return splice(data, list_of_files, queue_path, None, max_havoc_cycles)
split_last_byte = f_diff + RAND(l_diff - f_diff)
block = data[f_diff:f_diff+split_last_byte]
content_target = content_target[:f_diff] + block + content_target[f_diff+split_last_byte:]
data = content_target
data, res = havoc(data, 0, max_havoc_cycles)
return data, func_state
# calculate AFL-like performance score
def calculate_perf_score(exec_per_sec, avg_exec_per_sec, bitmap_size, avg_bitmap_size, handicap):
perf_score = 100
if exec_per_sec * 0.1 > avg_exec_per_sec: perf_score = 10
elif exec_per_sec * 0.25 > avg_exec_per_sec: perf_score = 25
elif exec_per_sec * 0.5 > avg_exec_per_sec: perf_score = 50
elif exec_per_sec * 0.75 > avg_exec_per_sec: perf_score = 75
elif exec_per_sec * 4 < avg_exec_per_sec: perf_score = 300
elif exec_per_sec < avg_exec_per_sec: perf_score = 200
elif exec_per_sec * 2 < avg_exec_per_sec: perf_score = 150
if bitmap_size * 0.3 > avg_bitmap_size: perf_score *= 3;
elif bitmap_size * 0.5 > avg_bitmap_size: perf_score *= 2;
elif bitmap_size * 0.75 > avg_bitmap_size: perf_score *= 1.5;
elif bitmap_size * 3 < avg_bitmap_size: perf_score *= 0.25;
elif bitmap_size * 2 < avg_bitmap_size: perf_score *= 0.5;
elif bitmap_size * 1.5 < avg_bitmap_size: perf_score *= 0.75;
if handicap > 4:
perf_score *= 4
# TODO: handicap -= 4
elif handicap:
per_score *= 2
#TODO handicap -= 1
return perf_score
# Calculates maximum number of cycles
def get_havoc_cycles(exec_per_sec, perf_score, splice):
havoc_div = 1
if exec_per_sec < 20:
havoc_div = 10
elif exec_per_sec >= 20 and exec_per_sec < 50:
havoc_div = 5
elif exec_per_sec >= 50 and exec_per_sec < 100:
havoc_div = 2
# from AFL source code
# stage_max = (doing_det ? HAVOC_CYCLES_INIT : HAVOC_CYCLES) * perf_score / havoc_div / 100;
# doing_det is -d flag of afl (not supported)
if splice:
stage_max = AFL_SPLICE_HAVOC * perf_score / havoc_div / 100;
else:
stage_max = AFL_HAVOC_CYCLES * perf_score / havoc_div / 100
# if not splice:
# TODO: if (queued_paths != havoc_queued)
# if we see more paths found by havoc spend more time
#if perf_score <= AFL_HAVOC_MAX_MULT * 100:
# stage_max *= 2
# perf_score *= 2
return stage_max, perf_score
class AFLFuzzer(object):
def __init__(self, user_tokens_dict, queue_path, file_name):
global tokens_list, tokens_list_length
self.possible_stages = OrderedDict()
# The order of this functions is super important. Splice must be the last and
# havoc must be before splice.
self.list_of_functions = [bitflip_1bit, bitflip_2bits, bitflip_4bits,
byteflip_1, byteflip_2, byteflip_4,
mutate_byte_arithmetic, mutate_2bytes_arithmetic, mutate_4bytes_arithmetic,
mutate_1byte_interesting, mutate_2bytes_interesting, mutate_4bytes_interesting,
dictionary_overwrite, dictionary_insert, havoc, splice]
self.current_function = self.list_of_functions[0]
self.file_name = file_name
self.current_result = None
self.current_function_id = 0
self.total_func_count = len(self.list_of_functions)
self.queue_path = queue_path
tokens_list = user_tokens_dict
tokens_list_length = len(tokens_list)
self.new_havoc_cycle = True
self.perf_score = 100
self.orig_perf_score = 100
def save_state(self, output_path):
# we need to save current_function_id and current_result
fd = open(output_path + "/afl_state_%s" % self.file_name, 'w')
if not fd:
WARNING(None, "Failed to save state of the AFLFuzzer for %s" % self.file_name)
fd.write("%d %s" % (self.current_function_id, str(self.current_result)))
fd.close()
def restore_state(self, output_path):
# we need to load current_function_id and current_result
INFO(1, None, None, "Loading AFL state from %s" % (output_path + "/afl_state_" + self.file_name))
try:
fd = open(output_path + "/afl_state_%s" % self.file_name, 'r')
except FileNotFoundError:
WARNING(None, "Unable to find %s file" % output_path + "/afl_state_%s" % self.file_name)
return # it can happen when we just found the file and user raised ctrl+c
if not fd:
WARNING(None, "Failed to load state of the AFLFuzzer for %s, will start from the beginning" % self.file_name)
return
content = fd.read()
function_id = content[:content.find(" ")]
self.current_function_id = int(function_id)
state = content[content.find(" ")+1:]
if "[" in state:
state = ast.literal_eval(state)
elif "None" in state:
state = None
else:
state = int(state)
self.current_result = state
self.current_function = self.list_of_functions[self.current_function_id % len(self.list_of_functions)]
fd.close()
INFO(1, None, None, "%s %s %s" % (self.file_name, self.current_result, self.current_function))
def mutate(self, data, list_of_files, exec_per_sec, avg_exec_per_sec, bitmap_size,
avg_bitmap_size, handicap):
if len(data) <= 0:
return data
if not self.current_result:
self.current_function = self.list_of_functions[self.current_function_id % len(self.list_of_functions)]
INFO(1, None, None, "Running %s stage of AFL mutator" % self.current_function)
if (self.current_function_id % self.total_func_count) == (self.total_func_count - 1): # if splice
if self.new_havoc_cycle:
self.havoc_max_stages, self.perf_score = get_havoc_cycles(exec_per_sec, self.perf_score, True)
self.new_havoc_cycle = False
# FYI: we are sending copy of list_of_files instead of actual list_of_files in slice
data, self.current_result = self.current_function(data, list(list_of_files),
self.queue_path,
self.current_result,
self.havoc_max_stages)
if not self.current_result:
self.new_havoc_cycle = True
self.perf_score = self.orig_perf_score
elif (self.current_function_id % self.total_func_count) == (self.total_func_count - 2): # if havoc
if self.new_havoc_cycle:
self.perf_score = calculate_perf_score(exec_per_sec, avg_exec_per_sec, bitmap_size,
avg_bitmap_size, handicap)
self.orig_perf_score = self.perf_score
self.havoc_max_stages, self.perf_score = get_havoc_cycles(exec_per_sec, self.perf_score, False)
self.new_havoc_cycle = False
data, self.current_result = self.current_function(data, self.current_result,
self.havoc_max_stages)
if not self.current_result:
self.new_havoc_cycle = True
self.perf_score = self.orig_perf_score
else:
data, self.current_result = self.current_function(data, self.current_result)
if not self.current_result:
self.current_function_id += 1
return data