-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaultinject.py
189 lines (166 loc) · 5.86 KB
/
faultinject.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
import sys
import os
import subprocess
import time
import random
import pexpect
import configure
import re
randinst_lib = "obj-intel64/randomInst.so"
randinst_config = "-randinst"
iterationinst = "obj-intel64/determineInst.so"
iterationinst_config1 = "-pc"
iterationinst_config2 = "-randinst"
nextinst = "obj-intel64/findnextinst.so"
nextinst_config1 = "-pc"
binary = ""
timeout = 500
instructionfile = "instruction"
iterationfile = "iteration"
nextpcfile = "nextpc"
stacksize = "spsize"
class FaultInjector:
def __init__(self,totalInst):
self.totalInst = totalInst
self.flag = 32
@property
def getBreakpoint(self):
## get
"""
:rtype: strings
"""
randomnum = random.randint(0,self.totalInst)
execlist = [configure.pin_home,"-t",os.path.join(configure.toolbase,randinst_lib),randinst_config,str(randomnum),"--",configure.benchmark]
for item in configure.args:
execlist.append(item)
self.execute(execlist)
# check if the file is generated
if not os.path.isfile(instructionfile):
print "No File generated!"
sys.exit(1)
regmem = ""
reg = ""
pc = ""
iteration = ""
with open(instructionfile,"r") as f:
lines = f.readlines()
for line in lines:
line = line.rstrip("")
if "REGNOTVALID" in line:
print "REG not valid! Exit"
sys.exit(1)
if "mem:" in line:
regmem = line.split(":")[1].rstrip("\n")
if "reg:" in line:
reg = line.split(":")[1].rstrip("\n")
if "pc:" in line:
pc = line.split(":")[1].rstrip("\n")
#if "next:" in line:
# next = line.split(":")[1]
if reg == "" and regmem == "":
print "No reg, Exit"
return []
if reg.startswith("r") or regmem.startswith("r"):
self.flag = 64
execlist = [configure.pin_home,"-t",os.path.join(configure.toolbase,iterationinst),iterationinst_config1,str(pc),iterationinst_config2,str(randomnum),"--",configure.benchmark]
for item in configure.args:
execlist.append(item)
self.execute(execlist)
if not os.path.isfile(iterationfile):
print "No iteration file generated! Exit"
return []
with open(iterationfile,"r") as f:
lines = f.readlines()
for line in lines:
line = line.rstrip("\n")
iteration = line
return [regmem,reg,pc, iteration]
def execute(self,execlist):
"""
:rtype: object
"""
print ' '.join(execlist)
p = subprocess.Popen(execlist)
elapsetime = 0
while (elapsetime < timeout):
elapsetime += 1
time.sleep(1)
#print p.poll()
if p.poll() is not None:
print "\t program finish", p.returncode
print "\t time taken", elapsetime
return str(p.returncode)
print "\tParent : Child timed out. Cleaning up ... "
p.kill()
return "timed-out"
#should never go here
def generateFaults(self,ori_value):
## it is complicated because if it is a 0x then non-digital chars are allowed, need a complicated regex.
if "0x" in ori_value:
res = re.findall('0[xX]?[A-Fa-f0-9]+',ori_value)
ori_value = res[0]
else:
ori_value = re.sub("\D","",ori_value)
bitsize = 31
if self.flag == 64:
bitsize = 63
pos = random.randint(0,bitsize)
mask = (1 << pos)
decvalue = 0
if "0x" in ori_value:
decvalue = int(ori_value,16)
else:
decvalue = int(ori_value)
print "New value is "+str(decvalue^mask)+" Old value is "+str(decvalue)
return str(decvalue^mask)
def get_stack_size(self):
size = ""
with open(stacksize,"r") as f:
lines = f.readlines()
for line in lines:
line = line.rstrip("\n")
if "," in line:
items = line.split(",")
size = items[len(items)-1]
return size
def getNextPC(self,pc):
execlist = [configure.pin_home,"-t",os.path.join(configure.toolbase,nextinst),nextinst_config1,str(pc),"--",configure.benchmark]
for item in configure.args:
execlist.append(item)
self.execute(execlist)
if not os.path.isfile(nextpcfile):
print "No nextpc file is generated! Exit"
return []
nextpc = ""
regw = []
stack = ""
flag = 0 # stackw: 1, stackr: 2 , nostack: 3
displacement = 0
scale = 0
index = ""
base = ""
with open(nextpcfile,"r") as f:
lines = f.readlines()
for line in lines:
line = line.rstrip("\n")
if "nextpc:" in line:
nextpc = line.split(":")[1]
if "regw" in line:
regw.append(line.split(":")[1])
if "stackr:" in line:
stack = line.split(":")[1]
flag = 2
if "stackw:" in line:
stack = line.split(":")[1]
flag = 1
if "nostack" in line:
flag = 3
if "base:" in line:
base = line.split(":")[1]
if "index:" in line:
index = line.split(":")[1]
if "displacement:" in line:
displacement = line.split(":")[1]
if "scale:" in line:
scale = line.split(":")[1]
return [nextpc,regw,stack,flag,base,index,displacement,scale]