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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#! /bin/python3
import re
import sys
import shutil
import argparse
from functools import partial as bind
from utils import check_version
from utils import print_separator
from run import Runner
class ContextBlock:
regnames = ['PC',
'RAX',
'RBX',
'RCX',
'RDX',
'RSI',
'RDI',
'RBP',
'RSP',
'R8',
'R9',
'R10',
'R11',
'R12',
'R13',
'R14',
'R15',
'flag ZF',
'flag CF',
'flag OF',
'flag SF',
'flag PF',
'flag DF']
def __init__(self):
self.regs = {reg: None for reg in ContextBlock.regnames}
def set(self, idx: int, value: int):
self.regs[list(self.regs.keys())[idx]] = value
def __repr__(self):
return self.regs.__repr__()
class Constructor:
def __init__(self, structure: dict):
self.cblocks = []
self.structure = structure
self.patterns = list(self.structure.keys())
def match(self, line: str):
# find patterns that match it
regex = re.compile("|".join(self.patterns))
match = regex.match(line)
idx = self.patterns.index(match.group(0)) if match else 0
pattern = self.patterns[idx]
register = ContextBlock.regnames[idx]
return register, self.structure[pattern](line)
def add(self, key: str, value: int):
if key == 'PC':
self.cblocks.append(ContextBlock())
if self.cblocks[-1].regs[key] != None:
raise RuntimeError("Reassigning register")
self.cblocks[-1].regs[key] = value
def parse(lines: list, labels: list):
ctor = Constructor(labels)
regex = re.compile("|".join(ctor.patterns))
lines = [l for l in lines if regex.match(l) is not None]
for line in lines:
key, value = ctor.match(line)
ctor.add(key, value)
return ctor.cblocks
def get_labels():
split_value = lambda x,i: int(x.split()[i], 16)
split_first = bind(split_value, i=1)
split_second = bind(split_value, i=2)
split_equal = lambda x,i: int(x.split('=')[i], 16)
labels = {'INVOKE': bind(split_equal, i=1),
'RAX': split_first,
'RBX': split_first,
'RCX': split_first,
'RDX': split_first,
'RSI': split_first,
'RDI': split_first,
'RBP': split_first,
'RSP': split_first,
'R8': split_first,
'R9': split_first,
'R10': split_first,
'R11': split_first,
'R12': split_first,
'R13': split_first,
'R14': split_first,
'R15': split_first,
'flag ZF': split_second,
'flag CF': split_second,
'flag OF': split_second,
'flag SF': split_second,
'flag PF': split_second,
'flag DF': split_second}
return labels
def equivalent(val1, val2):
return val1 == val2
def verify(translation: ContextBlock, reference: ContextBlock):
if translation.regs["PC"] != reference.regs["PC"]:
return 1
print_separator()
print(f'For PC={hex(translation.regs["PC"])}')
print_separator()
for el1 in translation.regs.keys():
for el2 in reference.regs.keys():
if el1 != el2:
continue
if translation.regs[el1] is None:
print(f'Element not available in translation: {el1}')
continue
if reference.regs[el2] is None:
print(f'Element not available in reference: {el2}')
continue
if not equivalent(translation.regs[el1], reference.regs[el2]):
print(f'Difference for {el1}: {hex(translation.regs[el1])} != {hex(reference.regs[el2])}')
return 0
def compare(txl: list, native: list, stats: bool = False):
txl = parse(txl, get_labels())
native = parse(native, get_labels())
if len(txl) != len(native):
print(f'Different number of blocks discovered translation: {len(txl)} vs. '
f'reference: {len(native)}', file=sys.stderr)
unmatched_pcs = {}
for translation, reference in zip(txl, native):
if verify(translation, reference) == 1:
# TODO: add verbose output
print_separator(stream=sys.stderr)
print(f'No match for PC {hex(translation.regs["PC"])}', file=sys.stderr)
if translation.regs['PC'] not in unmatched_pcs:
unmatched_pcs[translation.regs['PC']] = 0
unmatched_pcs[translation.regs['PC']] += 1
if stats:
print_separator()
print('Statistics:')
print_separator()
for pc in unmatched_pcs:
print(f'PC {hex(pc)} unmatched {unmatched_pcs[pc]} times')
return 0
def read_logs(txl_path, native_path, program):
txl = []
with open(txl_path, "r") as txl_file:
txl = txl_file.readlines()
native = []
if program is not None:
runner = Runner(txl, program)
native = runner.run()
else:
with open(native_path, "r") as native_file:
native = native_file.readlines()
return txl, native
def parse_arguments():
parser = argparse.ArgumentParser(description='Comparator for emulator logs to reference')
parser.add_argument('-p', '--program',
type=str,
help='Path to oracle program')
parser.add_argument('-r', '--ref',
type=str,
required=True,
help='Path to the reference log (gathered with run.sh)')
parser.add_argument('-t', '--txl',
type=str,
required=True,
help='Path to the translation log (gathered via Arancini)')
parser.add_argument('-s', '--stats',
action='store_true',
default='store_false',
help='Run statistics on comparisons')
parser.add_argument('-v', '--verbose',
action='store_true',
default='store_true',
help='Path to oracle program')
args = parser.parse_args()
return args
if __name__ == "__main__":
check_version('3.7')
args = parse_arguments()
txl_path = args.txl
native_path = args.ref
program = args.program
stats = args.stats
verbose = args.verbose
if program is None and native_path is None:
raise ValueError('Either program or path to native file must be'
'provided')
txl, native = read_logs(txl_path, native_path, program)
if program != None and native_path != None:
with open(native_path, 'w') as w:
w.write(''.join(native))
compare(txl, native, stats)
|