Skip to content

Commit 510629d

Browse files
committed
remove bad shrinking change
1 parent 7374797 commit 510629d

1 file changed

Lines changed: 39 additions & 50 deletions

File tree

fuzzer.py

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,38 @@ class Fuzzer:
5252
The main fuzzer object.
5353
"""
5454

55-
def __init__(self, target, corpus_dir, shrinking=False):
55+
def __init__(self, target, corpus_dir):
5656
# io.BytesIO = ByteIOFeedback
5757
self.target = target
5858
self.corpus = []
5959
self.corpus_dir = corpus_dir
6060
# filename -> edges
6161
self.edges = defaultdict(set)
6262
to_import = list(corpus_dir.iterdir())
63-
if not shrinking:
64-
for path in to_import:
65-
try:
66-
self.import_testcase(path)
67-
except Exception as exc:
68-
print("{} crashes `{}`, please fix.".format(
69-
path, path.read_bytes()))
70-
raise exc
71-
if not to_import:
72-
self.test_one_input(b'A' * 64)
73-
if not self.edges:
74-
logging.error("No coverage found! "
75-
"Does your target function do something?")
76-
exit()
77-
78-
def import_testcase(self, path, shrinking=False):
63+
for path in to_import:
64+
try:
65+
self.import_testcase(path)
66+
except Exception as exc:
67+
print("{} crashes `{}`, please fix.".format(path,
68+
path.read_bytes()))
69+
raise exc
70+
if not to_import:
71+
self.test_one_input(b'A' * 64)
72+
if not self.edges:
73+
logging.error("No coverage found! "
74+
"Does your target function do something?")
75+
exit()
76+
77+
def import_testcase(self, path):
7978
testcase = path.read_bytes()
80-
self.test_one_input(testcase, shrinking=shrinking)
79+
self.test_one_input(testcase)
8180

8281
@property
8382
def smallest_input(self):
8483
assert self.corpus
8584
return min(len(e) for e in self.corpus)
8685

87-
def test_one_input(self, data, shrinking=False):
86+
def get_edges_from_input(self, data):
8887
tracer = Tracer()
8988
tracer.start()
9089
crashed = False
@@ -93,32 +92,27 @@ def test_one_input(self, data, shrinking=False):
9392
except Exception as exc:
9493
# TODO: when shrinking check exception matches?
9594
crashed = True
96-
if not shrinking:
97-
self.write_crash_to_disk(data)
98-
raise exc
95+
self.write_crash_to_disk(data)
96+
raise exc
9997
tracer.stop()
98+
return tracer.edges, crashed
99+
100+
def test_one_input(self, data):
101+
tracer_edges, _ = self.get_edges_from_input(data)
100102
has_new = False
101-
for name, edges in tracer.edges.items():
103+
for name, edges in tracer_edges.items():
102104
if edges is None:
103105
continue
104106
edges = set(edges)
105107
if edges - self.edges[name]:
106108
has_new = True
107109
self.edges[name] |= edges
108-
if shrinking and not crashed:
109-
return False
110-
if has_new or (shrinking and (not self.corpus or
111-
len(data) < min(len(e)
112-
for e in self.corpus))):
110+
if has_new:
113111
# if self.corpus:
114112
# print(len(data), min(len(e) for e in self.corpus))
115113
self.corpus.append(data)
116-
dest = self.write_to_disk(bytes(data))
117-
if shrinking:
118-
assert crashed
119-
print("Wrote crasher: {} @ {}".format(len(data), dest))
120-
return True
121-
return False
114+
self.write_to_disk(bytes(data))
115+
return has_new
122116
# print(cov_data.measured_files())
123117

124118
# def mutate_shuffle(data):
@@ -129,7 +123,6 @@ def write_to_disk(self, data):
129123
dest = self.corpus_dir.joinpath(name)
130124
if not dest.exists():
131125
dest.write_bytes(data)
132-
return dest
133126

134127
def write_crash_to_disk(self, data):
135128
name = 'crash-' + hashlib.sha1(data).hexdigest()
@@ -246,37 +239,33 @@ def generate_input(self):
246239
assert False
247240
return bytes(data) # ByteFeedback(data)
248241

249-
def print_status(self, info, num_execs, start, shrinking=False):
242+
def print_status(self, info, num_execs, start):
250243
elapsed = max(int(time.time() - start), 1)
251244
exec_s = num_execs // elapsed
252-
if shrinking:
253-
cov = min(len(e) for e in self.corpus)
254-
else:
255-
cov = sum(len(edges) for edges in self.edges.values())
245+
cov = sum(len(edges) for edges in self.edges.values())
256246
print("#{} {} cov: {} corpus: {} exec/s: {}".format(
257247
num_execs, info, cov, len(self.corpus), exec_s))
258248

259-
def print_pulse(self, num_execs, start, shrinking=False):
260-
self.print_status("pulse", num_execs, start, shrinking=shrinking)
249+
def print_pulse(self, num_execs, start):
250+
self.print_status("pulse", num_execs, start)
261251

262-
def print_new(self, num_execs, start, shrinking=False):
263-
self.print_status("NEW", num_execs, start, shrinking=shrinking)
252+
def print_new(self, num_execs, start):
253+
self.print_status("NEW", num_execs, start)
264254

265-
def fuzz(self, shrinking=False):
255+
def fuzz(self):
266256
num_execs = 0
267257
start = time.time()
268258
while True:
269259
data = self.generate_input()
270-
has_new = self.test_one_input(data, shrinking=shrinking)
260+
has_new = self.test_one_input(data)
271261
if has_new:
272-
self.print_new(num_execs, start, shrinking=shrinking)
262+
self.print_new(num_execs, start)
273263
elif bin(num_execs).count("1") == 1:
274-
self.print_pulse(num_execs, start, shrinking=shrinking)
264+
self.print_pulse(num_execs, start)
275265
num_execs += 1
276266

277267
def minimize(self, path):
278-
self.import_testcase(path, shrinking=True)
279-
self.fuzz(shrinking=True)
268+
self.import_testcase(path)
280269

281270

282271
if __name__ == '__main__':

0 commit comments

Comments
 (0)