forked from Colloportus0/MLIRSmith11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milr-fuzz.py
76 lines (67 loc) · 2.34 KB
/
milr-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
import os
import numpy as np
import random
import time
opts = []
with open('opt.txt', 'r') as f:
for opt in f.readlines():
end = opt.find('\n')
opts.append(opt[:end].strip())
# replace the executable with the local version with commit-id
gen_executable = "toyc-ch5"
opt_executable = "../bin/mlir-opt"
mlir_dir = "mlir/"
gen_fail_file = "genfail/"
crash_dir = "crash/"
log_dir = "errlog/"
temp_log = "temp.log"
src_mlir = "src.mlir"
tests_per_file = 100
opts_per_file = 10
def exec_cmd(cmd):
return os.system(cmd) >> 8
mlir_file_idx = 0
# main loop
while True:
r = random.random()
mlir_file = mlir_dir + "temp" + str(mlir_file_idx) + str(hash(r)) + ".mlir"
mlir_file_idx = mlir_file_idx + 1
# generate new temp mlir
gen_cmd = gen_executable + " -emit=mlir-affine " + src_mlir + " 2>" + mlir_file
gen_stat = exec_cmd(gen_cmd)
if gen_stat > 0:
gen_log_file = gen_fail_file + mlir_file[len(mlir_dir):] + ".log"
exec_cmd("mv " + mlir_file + " " + gen_log_file)
continue
print(gen_cmd)
# see if the pass can run successfully instead of crash
succeed_opts = []
print(len(opts))
for opt in opts:
cmd = opt_executable + " " + opt + " " + mlir_file + " 1>/dev/null 2>" + temp_log
state = exec_cmd(cmd)
print(str(state) + ": "+cmd)
if state > 130:
crashlog = crash_dir + mlir_file[len(mlir_dir):] + str(hash(opt)) + ".log"
cmd = "mv " + temp_log + " " + crashlog
print(cmd)
os.system(cmd)
else :
succeed_opts.append(opt)
# test the generated file
for i in range(0, tests_per_file):
idxes = np.random.randint(0, len(succeed_opts), opts_per_file)
np.random.shuffle(idxes)
cmd = opt_executable
opt_str = ""
for idx in idxes:
opt_str = opt_str + succeed_opts[idx]
cmd = cmd + " " + succeed_opts[idx]
cmd = cmd + " " + mlir_file + " 1>/dev/null 2>" + temp_log
state = exec_cmd(cmd)
print(str(state) + ": " + cmd)
if state > 130:
crashlog = crash_dir + mlir_file[len(mlir_dir):] + str(hash(opt_str)) + ".log"
cmd = "mv " + temp_log + " " + crashlog
print(cmd)
os.system(cmd)