-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutate.py
188 lines (155 loc) · 5.72 KB
/
mutate.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
import json5 as json
import random
import math
import os
import subprocess
import ipdb
class Mutate():
def __init__(self, reinforcename, config="config.json5"):
self.reinforcename = reinforcename
self.config = config
self.preparser_path = None
self.gdbscript_path = None
self.llvm_home = None
self.clang = None
self.opt = None
self.llc = None
self.plugin_path = None
self.reinforce_strategies = None
self.policies = None
try:
self.parse_config()
except Exception as e:
print("parse json5 config error: %s", e)
ipdb.post_mortem()
try:
self.gen_policies()
self.c2ir("tests/input.c", "tests/input.ll")
self.collect_meta("tests/input.ll", "tests")
self.ir2obj("tests/input.ll", "tests/input.o" )
self.obj2exe("tests/input.o", "tests/a.out")
self.collect_dynvalue("tests/a.out", "bcftest", "before_dynvalue.json5", "tests/input.json5")
##############################################################
self.sub_mutate("tests/input.ll", "tests/output.ll")
self.ir2obj("tests/output.ll", "tests/output.o" )
self.obj2exe("tests/output.o", "tests/b.out")
self.collect_dynvalue("tests/b.out", "bcftest", "after_dynvalue.json5", "tests/input.json5")
except Exception as e:
print("get mutate error: %s", e)
ipdb.post_mortem()
def parse_config(self):
with open(self.config, "r", encoding='utf-8') as f:
json_data = json.load(f)
self.reinforce_strategies = json_data[self.reinforcename]["policies"]
self.plugin_path = json_data[self.reinforcename]["plugin_path"]
if not os.path.exists(self.plugin_path):
raise Exception("can't find plugin path for {}".format(self.reinforcename))
if "llvm_home" in json_data and os.path.exists(json_data["llvm_home"]):
self.llvm_home = json_data["llvm_home"]
elif os.environ.get("LLVM_HOME") is not None:
self.llvm_home = os.environ.get("LLVM_HOME")
else:
raise Exception("get ndk_home error")
#self.llvm_home = "/usr/"
self.clang = os.path.join(self.llvm_home, "bin/clang")
if not os.path.exists(self.clang):
raise Exception("can't find clang in ndk home")
self.opt = os.path.join(self.llvm_home, "bin/opt")
if not os.path.exists(self.opt):
raise Exception("can't find opt in ndk home")
self.llc = os.path.join(self.llvm_home, "bin/llc")
if not os.path.exists(self.llc):
raise Exception("can't find llc in ndk home")
if not "typeanalysis" in json_data:
raise Exception("can't find typeanalysis in config.json5")
elif not os.path.exists(json_data["typeanalysis"]):
raise Exception("typeanalysis path specify in config.json5 not exist")
else:
self.preparser_path = json_data["typeanalysis"]
if not "gdbscript" in json_data:
raise Exception("can't find gdbscript in config.json5")
elif not os.path.exists(json_data["gdbscript"]):
raise Exception("gdbscript path specify in config.json5 not exist")
else:
self.gdbscript_path = json_data["gdbscript"]
def gen_policies(self):
# TODO complicate
choices_num = math.ceil(1.2 * len(self.reinforce_strategies))
policies = random.choices(self.reinforce_strategies, k=choices_num)
self.policies = "-" + " -".join(policies)
def c2ir(self, input_c , input_ir):
cmd = [
self.clang,
"-g",
"-gdwarf-4",
"-S",
"-emit-llvm",
"-fno-discard-value-names",
input_c,
"-o",
input_ir
]
cmd = " ".join(cmd).split(" ")
subprocess.run(cmd)
def ir2obj(self, input_ir, input_obj):
cmd = [
self.llc,
"-filetype=obj",
input_ir,
"-o",
input_obj
]
cmd = " ".join(cmd).split(" ")
subprocess.run(cmd)
def obj2exe(self, input_obj, input_exe):
cmd = [
self.clang,
input_obj,
"-o",
input_exe
]
cmd = " ".join(cmd).split(" ")
subprocess.run(cmd)
def collect_meta(self, input_ir, genepath):
cmd = [
self.opt,
"-enable-new-pm=0",
"-load=" + self.preparser_path,
"-parser",
"-S",
input_ir,
"-o",
"tests/tmp.ll",
"-genepath=" + genepath
]
cmd = " ".join(cmd).split(" ")
subprocess.run(cmd)
def collect_dynvalue(self, exefile, debugged_fun, result_file, meta_file):
cmd = ['gdb',
'--batch',
'-ex=py exefile="{}";debugged_fun="{}";result_file="{}";meta_file="{}"'.format(exefile, debugged_fun, result_file, meta_file),
'-x',
self.gdbscript_path,
exefile]
subprocess.run(cmd)
def diff(self):
# TODO
pass
def sub_mutate(self, input_ir, output_ir):
cmd = [
self.opt,
"-enable-new-pm=0",
"-load=" + self.plugin_path,
self.policies,
"-S",
input_ir,
"-o",
output_ir
]
cmd = " ".join(cmd).split(" ")
subprocess.run(cmd)
if __name__ == "__main__":
try:
mutate = Mutate("reinforce")
except Exception as e:
ipdb.post_mortem()