-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreducer.py
executable file
·79 lines (61 loc) · 1.86 KB
/
reducer.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
#!/usr/bin/env python3
#*******************************************************************************
#* reducer.py
#*
#* Copyright (C) 2018 Marvin Löbel <[email protected]>
#*
#* All rights reserved. Published under the BSD-2 license in the LICENSE file.
#******************************************************************************/
import argparse
import subprocess
import copy
import random
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('cmd', nargs=argparse.REMAINDER)
args = parser.parse_args()
RF = "REDUCEFILE"
LRF = "LAST_REDUCEFILE"
def read_binary(path):
with open(path, 'rb') as file:
inp = file.read()
return inp
def write_binary(path, data):
with open(path, 'wb') as file:
file.write(data)
file.flush()
test = read_binary(path)
assert test == data
def run(cmd, current):
print(cmd)
print("Input size: {}".format(len(current)))
write_binary(RF, current)
print("------------------------------------")
res = subprocess.run(cmd)
print("------------------------------------")
if res.returncode != 0:
return True
else:
return False
inp = read_binary(args.filename)
write_binary(RF, inp)
cmd = list(args.cmd)
prev = copy.deepcopy(inp)
current = copy.deepcopy(inp)
while True:
print("====================================")
if run(cmd, current):
# reduce further
prev = copy.deepcopy(current)
write_binary(LRF, prev)
ri = random.randrange(0, len(current))
rl = random.randrange(0, (int(len(current) / 10)) + 1) + 1
left = current[:ri]
right = current[(ri+rl):]
current = left + right
else:
print("Did not fail, try something different")
if prev == current:
print("Got stuck")
exit(1)
current = copy.deepcopy(prev)